Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular-cli and bootstrap 4

I move my first steps with angular 2, in particular I use angular-cli official tool to create new project.

I created a new project in this way

ng new [my-project-name]

The project was created correctly.

After that I would like to install bootstrap 4, and I follow the official guide in angular-cli page.

I install bootstrap with npm:

npm install bootstrap@next

and I add the line in my angular-cli.json:

"scripts": [
  "../node_modules/jquery/dist/jquery.js",
  "../node_modules/tether/dist/js/tether.js",
  "../node_modules/bootstrap/dist/js/bootstrap.js"
],
"styles": [
  "styles.css",
  "../node_modules/bootstrap/dist/css/bootstrap.css"
],

in app object.

When I build the app and run it in server with: ng serve

I don't find in index.html the reference for bootstrap css and javascript.

I don't understand if import of this file is automatically added in my index.html or I need to add it manually.

like image 479
user3790694 Avatar asked Sep 12 '16 07:09

user3790694


People also ask

Can I use Angular and bootstrap together?

Yes, you can use parts of Angular Material and Bootstrap together in the same web or mobile project. Developers need to be careful not to use the same components, which can clash. Angular Material and Bootstrap offer a variety of unique components for great website design.

Which bootstrap version is compatible with Angular 12?

we use ng-bootstrap exclusively for our applications and we have following combinations in our applications: Angular 12 - bootstrap 4 (ng-bootstrap 10.

Can we use bootstrap in ionic 4?

Just paste the BootstrapCDN into the header of src/index. html. this will require internet connection on mobile device.. remember ionic is also for mobile apps which not necessary have internet connection.

How do I add bootstrap to Stackblitz?

First, add the bootstrap npm module to the dependencies section. Next, in the styles. css file, import the bootstrap styles. @import '~bootstrap/dist/css/bootstrap.


2 Answers

When you add the scripts and styles entries to the angular.cli.json file, they are added to the global scope. Specifically, they are added to scripts.bundle.js and styles.bundle.js. There's no need to add them to index.html. You should be good to go after stopping and restarting ng serve. For good measure I run an npm rebuild after making changes to the angular.cli.json.

like image 77
Larry Popiel Avatar answered Nov 01 '22 15:11

Larry Popiel


You don't need to add anything manually in the HTML file. It's done automatically for you.

Here's why:

Your CSS styles will be added to the styles.bundle.css file. So you will see the following link to stylesheet in your HTML source when you run the app: <link href="styles.bundle.css" rel="stylesheet">. If you click the styles.bundle.css in the browser, it will show you all the styles it has compiled together.

One more important thing. I also suggest you swap the order of the stylesheets in your angular-cli.json file to the following:

"styles": [
  "../node_modules/bootstrap/dist/css/bootstrap.css",
  "styles.css",
]

The reason for the above change is that your files are compiled and served in the order that you provide in angular-cli.json file. So let's say if you have written some custom styles for btn-primary in styles.css, that will be over-written by bootstrap.css always and you will never see your styles being applied in the UI and you won't be able to figure out what is going wrong.

If you swap the order of your files as I suggest, bootstrap styles will be loaded first, followed by your custom styles thereby, over-riding bootstrap's styles with your own styles. This would be the only fix you need to do in your case right now and everything else should work smoothly without any effort on your part.

like image 25
Devner Avatar answered Nov 01 '22 17:11

Devner