Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 501
Philipp Januskovecz Avatar asked Mar 29 '17 07:03

Philipp Januskovecz


People also ask

Can I use SCSS in CSS angular project?

With the help of Angular CLI, you can install CSS or SCSS on your project and start working on that in a suitable way. If you are working with the CSS or SCSS in your angular project then it is very easy for you as compared to most other frameworks.

How would you use Sass 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.


3 Answers

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.

like image 111
Chrillewoodz Avatar answered Oct 16 '22 21:10

Chrillewoodz


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+)

like image 34
T04435 Avatar answered Oct 16 '22 20:10

T04435


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" ], ...

like image 2
IndyWill Avatar answered Oct 16 '22 21:10

IndyWill