I'm new to Angular and I'm coming from the Ember community. Trying to use the new Angular-CLI based off of Ember-CLI.
I need to know the best way to handle SASS in a new Angular project. I tried using the ember-cli-sass
repo to see if it would play along since a number of core components of the Angular-CLI are run off of Ember-CLI modules.
It didnt work but than again not sure if I just misconfigured something.
Also, what is the best way to organize styles in a new Angular project? It would be nice to have the sass file in the same folder as the component.
Update for Angular 6+
New Projects
When generating a new project with Angular CLI, specify the css pre-processor as
Use SCSS syntax
Use SASS syntax
Updating existing project
Set default style on an existing project by running
Use SCSS syntax
Use SASS syntax
The above command will update your workspace file (angular.json) with
where
styleext
can be eitherscss
orsass
as per the choice.Original Answer (for Angular < 6)
New projects
For new projects we can set the options
--style=sass
or--style=scss
according to the desired flavor SASS/SCSSUse SASS syntax
Use SCSS syntax
then install
node-sass
,Updating existing projects
To make
angular-cli
to compile sass files withnode-sass
, I had to run,which installs
node-sass
. Thenfor SASS syntax
for SCSS syntax
to set the default styleExt to sass
(or)
change
styleExt
tosass
orscss
for desired syntax in.angular-cli.json
,for SASS syntax
for SCSS syntax
Although it generated compiler errors.
which was fixed by changing the lines of
.angular-cli.json
,to either,
for SASS syntax
for SCSS syntax
Quoted from Officials github repo here -
See here
ng set --global defaults.styleExt=scss
is deprecated since ng6. You will get this message:You should use:
If you want to target a specific project (replace {project} with your project's name):
Set it as Global Default
ng set defaults.styleExt=scss --global
CSS Preprocessor integration Angular CLI supports all major CSS preprocessors:
To use these preprocessors simply add the file to your component's styleUrls:
When generating a new project you can also define which extension you want for style files:
Or set the default style on an existing project:
note: @schematics/angular is the default schematic for the Angular CLI
Style strings added to the @Component.styles array must be written in CSS because the CLI cannot apply a pre-processor to inline styles.
Based on angular documentation https://github.com/angular/angular-cli/wiki/stories-css-preprocessors
When you are creating your project with angular cli try this:
This generating all your components with predifined sass files.
If you want scss syntax create your project with :
If you are changing your existing style in your project
Cli handles the rest of it.
For Latest Versions
For Angular 6 to set new style on existing project with CLI:
Or Directly into angular.json: