For Angular 6 check the Official documentation
Note: For @angular/cli versions older than 6.0.0-beta.6 use ng set in place of ng config.
For existing projects
In an existing angular-cli project that was set up with the default css styles you will need to do a few things:
- Change the default style extension to
scss
Manually change in
.angular-cli.json(Angular 5.x and older) orangular.json(Angular 6+) or run:ng config defaults.styleExt=scss
if you get an error: Value cannot be found. use the command:
ng config schematics.@schematics/angular:component.styleext scss
(*source: Angular CLI SASS options)
Rename your existing
.cssfiles to.scss(i.e. styles.css and app/app.component.css)Point the CLI to find styles.scss
Manually change the file extensions in
apps[0].stylesinangular.json
- Point the components to find your new style files
Change the
styleUrlsin your components to match your new file names
For future projects
As @Serginho mentioned you can set the style extension when running the ng new command
ng new your-project-name --style=scss
If you want to set the default for all projects you create in the future run the following command:
ng config --global defaults.styleExt=scss
Answer from Chic on Stack OverflowFor Angular 6 check the Official documentation
Note: For @angular/cli versions older than 6.0.0-beta.6 use ng set in place of ng config.
For existing projects
In an existing angular-cli project that was set up with the default css styles you will need to do a few things:
- Change the default style extension to
scss
Manually change in
.angular-cli.json(Angular 5.x and older) orangular.json(Angular 6+) or run:ng config defaults.styleExt=scss
if you get an error: Value cannot be found. use the command:
ng config schematics.@schematics/angular:component.styleext scss
(*source: Angular CLI SASS options)
Rename your existing
.cssfiles to.scss(i.e. styles.css and app/app.component.css)Point the CLI to find styles.scss
Manually change the file extensions in
apps[0].stylesinangular.json
- Point the components to find your new style files
Change the
styleUrlsin your components to match your new file names
For future projects
As @Serginho mentioned you can set the style extension when running the ng new command
ng new your-project-name --style=scss
If you want to set the default for all projects you create in the future run the following command:
ng config --global defaults.styleExt=scss
As of ng6 this can be accomplished with the following code added to angular.json at the root level:
Manually change in .angular.json:
"schematics": {
"@schematics/angular:component": {
"styleext": "scss"
}
}
» npm install schematics-scss-migrate
How to add scss in an existing angular project - Stack Overflow
Angular generates css instead of scss in library
I have configured my project with scss . now I want to use css in my project.how can I do this
Is it possible to have both css and scss in Angular - Stack Overflow
Videos
In the latest version of Angular (v13), It's npm library migrating from css to scss and vice versa
ng add schematics-scss-migrate
If you created your project choosing the default style option as CSS, it's possible that neither.
ng config defaults.styleExt=scss nor
ng config schematics.@schematics/angular:component.style scss
work, since the Angular CLI will try to change these options in your angular.json file, and they are not created at all with the default CSS option. If none of these options work as a first step, you can add the entry in your angular.json file yourself, under schematics:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"myProject": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"root": "",
"sourceRoot": "src",
...
This will of course only change the extension for the newly generated components. If you need to migrate your project, you would have to rename all your styles files changing the extension from CSS to SCSS, and also the reference to those files in the @Component directive of each component.
EDIT for Angular 6+
Found on GitHub page of Angular:
stylePreprocessorOptions is still supported. You will have to manually update the newly generated angular.json though. Its new place is inside the "options" block of the json.
An example config could look like this:
"options": {
"outputPath": "../webapp",
"index": "src/index.html",
"main": "src/main.ts",
"tsConfig": "src/tsconfig.app.json",
"polyfills": "src/polyfills.ts",
"assets": [
{
"glob": "**/*",
"input": "src/resources",
"output": "/resources"
},
],
"styles": [
"src/styles.scss"
],
"stylePreprocessorOptions": {
"includePaths": [
"src/styles"
]
},
"scripts": []
}
Note the changed path.
ORIGINAL answer
Actually it just went fine out-of-the-box by having the "styleExt" set to "css". A bunch of components in a specific module were using SASS. I have a sass folder with variables and mixins and this setting in angular-cli.json was needed to have these scss files compiled / processed correctly:
"stylePreprocessorOptions": {
"includePaths": [
"sass"
]
}
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.
You can have both but you have to set your style extension to SASS or CSS only.
Now of if you want to use CSS or SASS in your component just use Angular CLI to generate component like
For CSS
ng g component my-component --style=css
For SASS
ng g component my-component --style=sass
So set style extension to SASS and use CSS extensions also in your project