How do I add Sass compilation in Angular CLI 6: angular.json?

前端 未结 3 641
再見小時候
再見小時候 2021-01-30 02:02

I just created a new Angular project using the new Angular CLI 6.0, but I need to add Sass compilation to my project. I\'m aware you can set a flag when you create a project, bu

3条回答
  •  旧时难觅i
    2021-01-30 02:45

    In Angular 6, It's more easier then that. The compilation is handled automatically! How that ? To use scss at components level you need to name the file with the extension scss (same go for less, styl). And in your component.ts you change the corresponding style to scss too.

    Here a screenshoot from the doc:

    And to use Scss at a globale scale (src/styles.css), then you need to change styles.css to styles.scss (same go for less, styl ...etc). And then you go on angular.json and change the old styles.css to the new styles.scss value. As shown in the image bellow:

    Here the link for the doc for the first part for the ones that want to explore themselves: https://angular.io/guide/component-styles#non-css-style-files

    Now what about setting up, ng-cli so when generating new component the extension will be by default scss ? Use the ng config command as follow:

     ng config schematics.@schematics/angular:component.styleext scss
    

    That will update angular.json with:

    "schematics": {
        "@schematics/angular:component": {
            "styleext": "scss"
        }
    } 
    

    Which is the equivalent to the old angular-cli.json

        defaults: {
            "styleext": "scss"
        }
    

    That's for a project that is already started but not set to default to scss. If you are creating a new project then use the option --style to specify that as follow:

    ng new my-project --style=scss
    

    And you will not have to use the precedent command which is a config command that allow us to update the specified field in the angular.json file (because it will be already set).

提交回复
热议问题