Using global scss variables in my components styles.
angular-cli: 1.0.0-beta.6 node: 6.2.1 os: darwin x64
Hi I have bootstrap override varaibles.scss, which I want to use as a Global style for my app.
So, I depend on these variables on every component.scss I write. Is there a way I can add a global dependency of variables.scss.
Right now I do import the variables.scss manually in every scss file that require.
There isn't any such functionality in the CLI itself, no. There is sass compilation but that's it.
@a5hik A temp fix for that is to make a _varaibles.scss and import where ever you use your variables. Not the cleanest way.
What @AmarildoK suggested could actually be incorporated as a feature into angular CLI. There could be a shared scss file somewhere and new .scss files in generated components could use have an import line to include the shared scss file by default.
Yup. Certainly need this feature.
Still nothing?
Any update about this people?
Any update on this?
I'm quite new to frontend, but I'm trying to declare some global SCSS vars for my Angular app, and I just want to be sure that it's not intended that what I'm doing should work.
I'm running angular-cli 1.0.0-beta.21. In my angular-cli.json file, in apps[0].styles, I reference my styles.scss file.
styles.scss
@import url('https://fonts.googleapis.com/icon?family=Material+Icons');
@import url('https://fonts.googleapis.com/css?family=Droid+Sans:400,700');
@import '_vars';
@import 'reset';
@import 'global';
@import 'index';
_vars.scss
/* VARIABLES */
/* brand colors */
$color-accent: #2980b9;
$color-primary: #ecf0f1;
$color-subtle: #aaaaaa;
/* rarity */
$color-rarity-legendary: #4c139d;
$color-rarity-ascended: #fb3e8d;
$color-rarity-exotic: #ffa405;
When I try to reference a variable declared this way in a component's .scss file, the console reports that the variable hasn't been declared.
Is this failing because of something I'm doing wrong, or is this not possible under the current version of the CLI?
Thanks so much for your time and help.
can someone explain to me how the SASS transpiling happens? I assume this happens:
- ng-cli transpiles global sass file to css
- find all component specific sass and transpile those to css
- concatenate all that css
is that correct?
I have the same problem with global sass variables. Also the transpile is for me at the moment a blackbox
What we do is create a folder called shared and under it a styles folder. We then create our sass there. Finally from your top level component typically "app.component.scss" just import.
@import './shared/styles/default';
Forgot you also on the top level component need to set View Encapsulation.
encapsulation: ViewEncapsulation.None


@greenchapter yeah.. I wish there was some good documentation for it. if you find anything, do let me know.
@ahmed-musallam see my above example I had forgotten the encapsulation flag in the root component. Just remember that the app.component.scss will not be encapsulated so any style imported there will propagate to any child component etc.
In may cases this works just fine, I see little issue with it. you can still have encapsulated styles for components.
Even with the encapsulation set I'm still getting a missing var error on compile. Is there something else that needs to be setup?
@askdesigners hard to say I'd need to see it. The above example I showed works just fine is it possible there's something up with your sass itself? Maybe post some screenshots or something and maybe something will stand out.
@origin1tech @askdesigners you should not disable encapsulation. It's just bad when you have leaking CSS.
If you know SASS, you know that variable and mixin declaration are NOT transpiled to CSS. So there is no issue with doing what @AmarildoK suggested above. just make a variables and mixins partial and include it when you needed. Since vars and mixins do not transpile unless you use them, there is no overhead here whatsoever.
What @a5hik is asking for in the thread is for convenience only... if you use angular, you are already familiar with angular's ceremonial steps for creating components and services.. this is the same thing... Additionally, I dont think the SASS transpiler supports injecting variables on the fly (during transpiling). the only way this can be done is by writing import statement into each sass file before SASS transpiling. And this can be done by a nodejs script if you must have that convenience.
@ahmed-musallam it's only a "leak" if you don't define it.
zero wrong with having a few styles that are global to your app for convenience. If that weren't the case cli wouldn't build with a global styles file pre-config'd nor would encapsulation option be avail. Disabling on a top level component does not negate encapsulation on sub-components.
If it's not what you prefer I understand but to emphatically suggest it's wrong is absurd. It would be unnecessary heavily lifting to have some separate build task/config just for those handful of global styles.
@origin1tech of course! you should use global CSS when you need global styles. Obviously no brainer there. And I would not repeat the same css in every component instead of using a global style.
but what your example, about setting encapsulation: ViewEncapsulation.none does not work as you described it nor was it intended to be used that way. the good folks at rangle.io explain the difference in the three encapsulation methods here: https://angular-2-training-book.rangle.io/handout/advanced-components/view_encapsulation.html. also Pascal Precht has a really good article over at thoughtram.io describing that as well: https://blog.thoughtram.io/angular/2015/06/29/shadow-dom-strategies-in-angular2.html
anyway, we are not talking about "global styles" in this thread, we are talking about "sass variables" and finding a way to reuse them across components.
What we do, is that we define a base.sass, that does not generates any CSS but only defines global variables.
This stylesheet is then imported on top of the component sass stylesheets.
@import "./src/assets/private/sass/base";
It does not generate any CSS overhead in the components, you can leave ViewEncapsulation enabled and all is good.
Ahh this is a frustrating problem. I want to just import colors and things one time. Not on every scss file.