How to configure Bootstrap 4 with Angular 8.3.6 using angular.json

Asked

Viewed 905 times

0

I am trying to configure Bootstrap 4 in Angular 8.3.6, I am following the following steps:

  • ng new novaAplicação (create the project)
  • npm install bootstrap jquery popper.js --save (install libraries)
  • set CSS and JS files in angular.json in the style and script arrays as below:

    "styles": [
      "node_modules/bootstrap/dist/css/bootstrap.css",
      "src/styles.scss"
    ],
    
    "scripts": [
      "node_modules/jquery/dist/jquery.js",
      "node_modules/popper.js/dist/umd/popper.js",
      "node_modules/bootstrap/dist/js/bootstrap.js"
    ]
    
  • Insert bootstrap test components into the app.component.html. In this case I put an Alert.

    A simple Primary Alert-check it out!

When I run the server (ng serve) only the text appears, without style formatting, when I use a component that has some kind of movement (dropdowns for example), this movement also does not work. It’s like you’re not recognizing the files (css, js) set in the angular.json.

The alternative seems to be to put the bootstrap links(css and js) directly in index.html, or, copy the files to the Assets folder, but I do not see how good practice seen the manual show the way I initially detailed.

Below the configuration:

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.803.6
@angular-devkit/build-angular     0.803.6
@angular-devkit/build-optimizer   0.803.6
@angular-devkit/build-webpack     0.803.6
@angular-devkit/core              8.3.6
@angular-devkit/schematics        8.3.6
@angular/cli                      8.3.6
@ngtools/webpack                  8.3.6
@schematics/angular               8.3.6
@schematics/update                0.803.6
rxjs                              6.4.0
typescript                        3.5.3
webpack                           4.39.2
  • But vc saw if there is an error in the browser console, if there was what error was? I don’t know if you noticed but the bootstrap’s Js file path on angular json. is with an invalid extension!

  • Hello Leandrade! There are no errors in the console when running the server, neither in the browser console nor in the terminal. Fixed here the file extension, I will fix here in the post, but it continues with the same problem, it seems that the information of arrays (scripts, Styles) do not migrate while running the server.

  • It’s José, in theory it was supposed to work, it seems that everything is configured correctly, I just can’t say 100% I never worked with Angular in version 8, It may be that I changed some configuration to use Bootstrap, pq until version 7, what you did was enough to be able to use.

  • I found some posts on the Angular forum reporting this problem in version 8 but they were closed without being treated, I will reopen there.

  • Did you try using scss? I did a project recently with it, but instead of putting it in angular.json, I put an import in my style.scss.

  • Yes, it is possible to work if you link the js and css files in index.html, or if css inserts an import in style.css. But it is a way to get around the problem, and not a solution according to the proposal of the tool, I think it is a bug, I reported on the forum of Angular, are checking, having the solution, I will put here.

Show 1 more comment

1 answer

1


In fact adding to the angular.json file the bootstrap does not load the styles. To solve the problem, it is necessary:

  1. Add the bootstrap scripts to your index.html after the app-root, inside the body:
<script
  src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
  integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
  crossorigin="anonymous"
></script>
<script
  src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
  integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
  crossorigin="anonymous"
></script>
  1. Import boostrap css into your Styles.css file:

    @import '.. /.. /node_modules/bootstrap/dist/css/bootstrap.css';

This way, you point directly to the boostrap CSS in your index.html. This would be a contouring solution to work the bootstrap with Angular.

  • Yes, I’m doing it in a palliative way. Thanks Giuliana.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.