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.
Tell angular-cli to always use scss by default
To avoid passing
--style scss
each time you generate a project, you might want to adjust your default configuration of angular-cli globally, with the following command:Please note that some versions of angular-cli contain a bug with reading the above global flag (see link). If your version contains this bug, generate your project with:
The following should work in an angular CLI 6 project. I.e if you are getting:
Then (making sure you change the project name)
To get your default project name use:
However: If you have migrated your project from <6 up to angular 6 there is a good chance that the config wont be there. In which case you might get:
Therefore a manual editing of
angular.json
will be required.You will want it to look something like this (taking note of the styleex property):
Seems like an overly complex schema to me. ¯_(ツ)_/¯
You will now have to go and change all your css/less files to be scss and update any references in components etc, but you should be good to go.
I noticed a very anoying gotcha in moving to scss files!!! One MUST move from a simple:
styleUrls: ['app.component.scss']
tostyleUrls: ['./app.component.scss']
(add./
) inapp.component.ts
Which ng does when you generate a new project, but seemingly not when the default project is created. If you see resolve errors during ng build, check for this issue. It only took me ~ 1 hour.
Best could be
ng new myApp --style=scss
Then Angular CLI will create any new component with scss for you...
Note that using
scss
not working in the browser as you probably know.. so we need something to compile it tocss
, for this reason we can usenode-sass
, install it like below:and you should be good to go!
If you using webpack, read on here:
from Angular starter here.
I tried to update my project to use the sass using this command
but got this
So I deleted my project (as I was just getting started and had no code in my project) and created a new one with sass initially set
But I would appreciate if someone can share a way to update the existing project as that would be nice.
Like Mertcan said, the best way to use scss is to create the project with that option:
Angular-cli also adds an option to change the default css preprocessor after the project has been created by using the command:
For more info you can look here for their documentation:
https://github.com/angular/angular-cli