Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular CLI SASS options

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.

like image 702
JDillon522 Avatar asked Mar 25 '16 12:03

JDillon522


People also ask

Can I use SCSS in angular?

In the Angular CLI, all components are self-contained and so are their Sass files. In order to use a variable from within a component's Sass file, you'll need to import the _variables. scss file. One way to do this is to @import with a relative path from the component.

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.


2 Answers

Angular CLI version 9 (used to create Angular 9 projects) now picks up style from schematics instead of styleext. Use the command like this:
ng config schematics.@schematics/angular:component.style scss
and the resulting angular.json shall look like this

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

Other possible solutions & explanations:

To create a new project with angular CLI with sass support, try this:

ng new My_New_Project --style=scss  

You can also use --style=sass & if you don't know the difference, read this short & easy article and if you still don't know, just go with scss & keep learning.

If you have an angular 5 project, use this command to update the config for your project.

ng set defaults.styleExt scss 

For Latest Versions

For Angular 6 to set new style on existing project with CLI:

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

Or Directly into angular.json:

"schematics": {       "@schematics/angular:component": {       "styleext": "scss"     } } 
like image 155
Mertcan Diken Avatar answered Oct 24 '22 15:10

Mertcan Diken


Update for Angular 6+

New Projects

When generating a new project with Angular CLI, specify the css pre-processor as

  • Use SCSS syntax

          ng new sassy-project --style=scss 
  • Use SASS syntax

          ng new sassy-project --style=sass 

Updating existing project

Set default style on an existing project by running

  • Use SCSS syntax

          ng config schematics.@schematics/angular:component.styleext scss 
  • Use SASS syntax

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

    The above command will update your workspace file (angular.json) with

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

where styleext can be either scss or sass as per the choice.

like image 44
All Іѕ Vаиітy Avatar answered Oct 24 '22 14:10

All Іѕ Vаиітy