In Angular 2 when we define a component we can specify a styleUrls
property to the decorator which points to a set of stylesheets to be applied to the component:
@Component({
selector: 'the-app'
templateUrl: 'templateFile.html',
styleUrls: ['componentStyles.css', 'moreComponentStyles.css']
})
export class TheAppComponent {}
Now, what if I want to write these styles with SASS?
I know I would need to use Gulp or Grunt to transpile the SCSS stylesheets to plain CSS. But this makes me confused on how we would correctly point Angular to the correct stylesheets.
How could I organize this workflow of using SASS with Gulp/Grunt together with Angular 2? How to use SASS to write the Angular 2 components styles?
You can use .scss in Component like this-
See if this helps.
After following this wiki, I got some
Error: Uncaught (in promise): Expected 'styles' to be an array of strings
which I resolved using this answer.Final solution is here:
home.component.ts:
webpack.conf.js: (part of it)
Command line inside project folder where your existing package.json is: npm install node-sass sass-loader raw-loader --save-dev
In webpack.common.js, search for "rules:" and add this object to the end of the rules array (don't forget to add a comma to the end of the previous object):
Then in your component:
I'm using it this way: