Philipp Januskovecz
Philipp Januskovecz

Reputation: 898

How to use global scss files in angular-cli project

I am new to Angular 4 and to the Angular-CLI. I cannot find any solution how to use root scss/css files for the whole application. Thereby, my question, how to use global scss files in the project?

Upvotes: 9

Views: 22888

Answers (4)

T04435
T04435

Reputation: 14022

You will need to add the following code to you angular-cli.json (angular.json on A6+)

"stylePreprocessorOptions": {
    "includePaths": [
      "../src/assets/scss/base"
    ]
  },

Where base is the folder containing the file/s you want to @import.

Then you can just

// BEFORE
@import '../../100TIMES/variables';
// NOW
@import 'variables'; // src/assets/scss/base/_variables.scss

Thanks.

Note: YOU NEED TO REBUILD THE PROJECT TO RELOAD angular-cli.json (angular.json on A6+)

Upvotes: 9

Chrillewoodz
Chrillewoodz

Reputation: 28368

Create a styles folder in the src folder, then you can import everything there into styles.scss which is also in the src folder. This is how mine looks:

// Vendors
@import './styles/vendors/_exports';

// Utils
@import './styles/utils/_exports';

// Globals
@import './styles/globals/_exports';

// Partials
@import './styles/partials/_exports';

// Components
@import './styles/components/_exports';

Every _exports file is importing all the files in the particular folder to make it easier to import it into the styles.scss file.

I hope this helps.

EDIT:

As pointed out in the comments, make sure that styles.scss is added to styles in the angular-cli.jsonfile or it will not work.

Upvotes: 17

IndyWill
IndyWill

Reputation: 2945

There is also the option to add your css/scss file to the "styles" array in the .angular-cli.json file. There may be a "best practice" that gets broken by doing this - I'm not really sure of the rules. But if you include a file in that array, it will be injected as though it were loaded at the top of index.html.

Here is an example from one of my projects: (/.angular-cli.json) ... "styles": [ "styles.scss", "../node_modules/font-awesome/scss/font-awesome.scss", "../node_modules/mdi/scss/materialdesignicons.scss" ], ...

Upvotes: 2

Vivek Doshi
Vivek Doshi

Reputation: 58593

Simplest way is to load scss file in the very root component , in this way all the children components will get the styles.

And if there are many files , we can import those scss files into the one scss file by using @import "scss-file";

And if children component doesn't want to use that style , then we can also handle that via encapsulation property of component

Another way is to :

stackoverflow.com/a/42865933/1876949

Upvotes: 0

Related Questions