There are other packages out there that you can use such as ngx-bootstrap, but this is the vanilla Bootstrap (including JavaScript) method.
There are some slight differences from versions before Angular 6. This post is for 6+ and will show you how to install Bootstrap 4 or 5 in Angular using either CSS or SCSS/SASS .
From the root of your project you need to install Bootstrap 5, and Popper.js which is used for dropdown menuβs and other JavaScript sort of things:
npm install bootstrap popper.js --S
Update your angular.json
file with the same code snippet below in two places: Underneath "build"
and "test"
:
"styles": [
"src/styles.css",
"./node_modules/bootstrap/dist/css/bootstrap.min.css",
],
"scripts": [
"./node_modules/popper.js/dist/umd/popper.min.js",
"./node_modules/bootstrap/dist/js/bootstrap.min.js"
]
Since we updated angular.json
youβll have to restart your server. Once you do that you should be good to go for the CSS version.
From the root of your project you need to install Bootstrap 5, and Popper.js which is used for dropdown menuβs and other JavaScript sort of things:
npm install bootstrap popper.js --S
Update your angular.json
file with the same code snippet below in two places. Underneath "build"
and "test"
:
"styles": [
"src/styles/styles.scss",
"./node_modules/bootstrap/scss/bootstrap.scss"
],
// See blurb about this below, and delete this comment.
"stylePreprocessorOptions": {
"includePaths": [
"src/styles",
"./node_modules/bootstrap/scss"
]
},
"scripts": [
"./node_modules/popper.js/dist/umd/popper.min.js",
"./node_modules/bootstrap/dist/js/bootstrap.min.js"
]
stylePreprocessorOptions
go to Angular/CLI README . It's a quick (3 min) read and definitely a good thing to be aware of if you're working with SASS, LESS, etc in Angular.
Create a styles
directory/folder in /src
. So it should be src/styles
.
Now move your styles.scss
file into that directory and create your variables, mixins and custom files to that src/styles
directory using the following naming convention: _variables.scss
, _mixins.scss
and _custom.scss
.
src
βββ app
βΒ Β βββ app-routing.module.ts
βΒ Β βββ app.component.html
βΒ Β βββ app.component.scss
βΒ Β βββ app.component.spec.ts
βΒ Β βββ app.component.ts
βΒ Β βββ app.module.ts
βΒ Β βββ users
βββ assets
βββ environments
βΒ Β βββ environment.prod.ts
βΒ Β βββ environment.ts
βββ favicon.ico
βββ index.html
βββ main.ts
βββ polyfills.ts
βββ styles
βΒ Β βββ _custom.scss /* Add files here but they should have the "_" pre-pended like `_custom.scss`, (shown above) other than `styles.scss`. */
βΒ Β βββ _mixins.scss /* Your editor and the lang strips these but I'm not entire sure why they are needed, but that's what I was taught. */
βΒ Β βββ styles.scss
βΒ Β βββ _variables.scss /* Add files here but they should have the "_" pre-pended like `_custom.scss`, (shown above) other than `styles.scss`. */
βββ test.ts
Now open your styles.scss
file and import Bootstrap and your custom/mixin/variable files:
/* Bootstrap */
@import 'bootstrap';
/* Custom */
@import 'variables';
@import 'mixins';
@import 'custom';
To use your variables in other components, youβll have to import them into every
component-name.component.scss
file like so. But because we imported all of Bootstrapβs .scss
files into our angular.json
file, we can leave off the path which makes things much cleaner:
/* component-name.component.scss */
@import 'variables';
Since we updated angular.json
youβll have to restart your server.