Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2: How to use bootstrap-sass with @extend and mixins in Angular-CLI?

I'm trying to use Twitter Bootstrap with Angular 2, SCSS and the scaffold generated by angular-cli

I want to apply the best practice to not litter my markup with Bootstrap-specific CSS classes. Instead I want to apply/use them via @extend or as mixins:

.mybutton {
   @extend .btn;
}

My problem:

  • The SASS compiler throws an error, because it does not know about .btn
  • If I add @import "bootstrap"; to my .scss-File, it will render the full Bootstrap-CSS in every component css

What I've done so far

  1. add @import "bootstrap"; to my app.component.scss
  2. Apply ViewEncapsulation.None to the AppComponent, so Angular 2 does not apply Shadow DOM emulation and the Bootstrap CSS applies to the whole application

     @Component({
        encapsulation: ViewEncapsulation.None
     })
    
  3. Include the following block into angular-cli-build.js, so relative @import will work

    sassCompiler: {
      cacheExclude: [/\/_[^\/]+$/],
      includePaths: [ 'node_modules/bootstrap-sass/assets/stylesheets' ]
    },
    vendorNpmFiles: [
        ...
        'bootstrap-sass/assets/**/*'           
    ]
    
like image 981
bentolor Avatar asked Jun 28 '16 09:06

bentolor


People also ask

Does bootstrap support Sass variables and Mixins?

Bootstrap includes a handful of Sass maps, key value pairs that make it easier to generate families of related CSS. We use Sass maps for our colors, grid breakpoints, and more. Just like Sass variables, all Sass maps include the ! default flag and can be overridden and extended.

Can we use both CSS and SCSS in angular?

The application renders fine with a mix of css and scss files. I assume this styleExt parameter is just there for the default component style ( css, scss, less ) file generation when adding components via angular-cli commands.

Can I use SCSS in angular?

SCSS Folder Structure Create a folder in your src project folder called styles . Move your styles. scss file into the newly created styles folder. Update your angular.

Can we use bootstrap and angular together?

The Bootstrap framework can be used together with modern JavaScript web & mobile frameworks like Angular. In the following you'll learn how to use the Bootstrap framework in your Angular project.


1 Answers

The newest version of the angular-cli works with webpack, and it has no angular-cli-build.js, so you can dismiss that if you upgrade.

I would also not disable the view encapsulation. If you need global styles, add them to a global stylesheet. (angular-cli creates one in your src folder, normally its a css file, so create your app with the -style=scss flag or change it in your angular-cli.json).

So, if you have the newest cli version, you can just import the bootstrap-sass mixins. E.g in your app.component.scss:

// core
@import '~bootstrap-sass/assets/stylesheets/bootstrap/variables';
@import '~bootstrap-sass/assets/stylesheets/bootstrap/mixins';
// buttons
@import '~bootstrap-sass/assets/stylesheets/bootstrap/buttons';

The 'core' imports are needed for sass to be able to compile your bootstrap. You probably should move these to their own mixin.

If you also want to use glyphicons, you also have to set the $icon-font-path bootstrap variable relative to the used scss file it´s used in. (e.g. $icon-font-path: '../node_modules/bootstrap-sass/assets/fonts/bootstrap/';)

like image 158
Stefan Negele Avatar answered Sep 27 '22 23:09

Stefan Negele