Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Angular 4 with SASS

I'm starting my first project using Angular4 and sass and I'm trying to figure it out how to work with sass the proper way. I'm using angular-cli and already set the default style to use scss syntax. I also already defined the compilation, etc.

However I saw that we have an option styleUrls on the component, where in some cases we define styles used only in that component.

The problem is, when I set a scss file in that component it's not generating it's own .css file neither being included in the main .css file, generated from the root project.

So basically I have a project structure like this:

app
    app.module.ts
    app.routing.ts
    home
        home.component.ts
        home.html
        _home.scss
    client
        client.component.ts
        client.html
        _client.scss
scss
    _imports.scss
    colors
        _colors.scss
    ui
        _button.scss
        _table.scss
    //.. more folder and files
styles.scss
index.html

And I'd like to be able to set the main css style within the styles.scss file, which will be inserted into the index.html file later, on the build process. But also be able to generate a single css file for each component I have, e.g. home and client, and only use those css styles within their own component.

Is it possible to do it? I've tried to do it and I didn't find any other resource about this!

like image 703
celsomtrindade Avatar asked Jun 22 '17 21:06

celsomtrindade


People also ask

How does angular integrate with sass?

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.

What is angular sass?

Angular Sass Example Well!! Sass (Syntactically Awesome Style Sheets) is an extension of CSS that allows you to use things like variables, nested rules, inline imports, and more. It also supports you to keep things organized and enables you to create style sheets faster.

Should I use CSS or SCSS in angular?

SCSS contains all the features of CSS and contains more features that are not present in CSS which makes it a good choice for developers to use it. SCSS is full of advanced features. SCSS offers variables, you can shorten your code by using variables. It is a great advantage over conventional CSS.


1 Answers

You can specify scss when you create project.

ng new project-name --style scss
like image 76
rch850 Avatar answered Oct 23 '22 05:10

rch850