You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Using global scss variables in my components styles. #1253
There isn't any such functionality in the CLI itself, no. There is sass compilation but that's it.
Reacted by Aleksei Lebedev, Tika Pahadi, Manish, waleed-shahzaib, Shubham Tiwari, Jeff, Maicon Wagner, msobolevdivorce, Alameer Ashraf Hassan, 天海るり and 7 more
Reacted by Kemal Beyaz
Reacted by waleed-shahzaib, Hemant Sankhla and Gabrielle Campagna
@a5hik A temp fix for that is to make a _varaibles.scss and import where ever you use your variables. Not the cleanest way.
Reacted by Pranjal Mittal, Grégoire Chauvet, N8 May, Ahmed Musallam, Martin Pultz, Luiz Mitidiero, Viktor Salnichenko, David Choi, Antoine D., Sebastian Marzec and 6 more
Reacted by waleed-shahzaib, Plavika, Rohan Shenoy and Sofía Ignacia Zambrano Mericq
Reacted by Yugloocamai, Anatoly Buchatsky, Rafael Bitencourt, Carlos Roso, DD, Luiz Mitidiero, Lluis Nieto, Hamza Hamidi, Alessandro Mitelli, Bohdan and 11 more
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.
Reacted by Bryan Jackson, Hugo Nogueira, cedvdb, Christian TD, Karel-Jan, Andrea, Royal Dev, Jason Cox, Rohan Shenoy and Ivan Sanchez
Reacted by Vincenzo Militello, Chris Brown, Connel Hooley, Antony Derham, Mateusz Gałganek, Ben Stein, Ramesh, Akkora, Grégoire Chauvet, Corbin Crutchley and 123 more
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.
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.
Reacted by Can Güney Aksakalli, Martin Schneider, nexus, Simba Cod3, Charles Harris, Michał Terczyński, hungry, Luca Basilico, Sune Brøndum Wøller, Thalaivar and 16 more
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
Reacted by Alex Grant, Thomas Ott, proAppleEater, Eduardo Bennertz, Corey Werner, rejidasan, Aaron Sherwood, Tingan Ho, Hossein Bajan, mariaalavez and 4 more
Reacted by dabder, Corsseir, Balaji Rajendran, flackjap, Anthony Johnston, Puria Rad Jahanbani, Andriy, sepo27, maxkuzmin, MOKHTARI Anouar and 32 more
Reacted by CodeCro, RobertBorkowskiITM and Off XaTA
Reacted by Ryan Cole, patricknazar, Ryan Donahue, Yevhenii Chubar, derek, Antoniy Zyumbyulev, Srinivas, Alejandro Ñáñez Ortiz, Aymeric Dominique, Biser Atanasov and 22 more
@daniel-seitz perhaps a bit of confusion -- this issue is going over many different topics.
Using @import “~variables.scss”; is great if your scss is inside of a folder within node_modules (unmodifiable).
Adding scss paths into angular.cli.json is required if you want "simple" imports for scss that are defined in your project.
Without adding the appropriate scss path(s) into angular.cli.json you will have to do relative path imports.
For example: @import "./src/assets/private/sass/base"; should be simplified into @import "base";
(assuming you've added the correct entry into .angular.cli.json)
As for automagically importing variables or base into all scss files, that may be a nice feature.
It could be done similar to this comment: webpack/sass-loader#218 (comment)
I do think if this feature was added it would need to be a new property of .angular.cli.json.
Thank you for the reply @hellofornow. I too would welcome this feature however from my current understanding its not trivial to implement this.
I was not clear enough with my question for you, so one clarification: Look out for the ~ symbol.
If one just imports like this @import “variables.scss”;, then yes this file needs to be inside node_modules - which is bad, don't do this.
BUT! if you do @import “~variables.scss”;, so with a ~, then you can import relative to your src folder. That's it, no other config. (Since we're in the angular-cli repo here I won't go into other structures and what we need to configure that 'home'-path)
Keeping that in mind, what's the need to do this then? I found no documentation.
Then I also did some tests (also with lazy loaded modules, ...) and - as we all know - we would not expect this to happen: Angular CLI with Sass compilation encapsulates the styles so that they won't have effects on other parts of the system.
When using import, we circumvent this and can use all variables, however when mixing variables with real css we also can cascade.
PROBLEM here is that each time use import, that 'real css' gets into the resulting file inline, over and over - which again goes against cascading and worse, makes our dist bigger.
The SOLUTION would be to make the variables (mixins, ...) available to the compilation so that it just knows about them and translating on the fly, nothing else.
I'd like to have these variables, or at least the starting point, in src/styles.scss as it is default but likely needs to be configurable.
If I find the time, I will dig deeper as I know that it's not just us who want this...
Sorry if this is off topic, but per my understanding in this thread, it seems that SASS process files more in isolation, then say LESS did, would that be correct?
In essence, the use case is of being able to use a @mixin which sets some color variables in one file at an "app" level, and hope for that cascade down into all the other components (even if node_modules).
@import"./styles/variables.scss";
@include setTheme('a'); //only changes red if thisis set manually,whichishardcodedandnotidealforareusablecomponentfromnpmh1 {
color: $color;
}
In this case, anything processed in my app component using $color would change red, but anything in my other components would not change color (would stay the default color of green) unless I manually call setTheme in all my components, which doesn't make them very reusable.
Thanks, and hope that wasn't too confusing! Any thoughts would be greatly appreciated!
Just an FYI, it looks like using the tilde to reference 'src' from the component level ala @import '~scss/variables'; is buggy (not working) in Angular 6 (issue). @import '~src/scss/variables'; and @import './src/scss/variables'; seem to work, though. Just a PSA if anyone runs into problems.
Reacted by Wes Kenyon, Marine Landraudie, david g., Gabrielle Campagna, Rien Heuver, Nguyen Minh Nhut, Owen Buckley, bmtheo and 忆年
this is ONE OF THE MOST COMMON THINGS I CAN IMAGINE. How can you get that wrong?!
also: you can make this work easily via webpack and sass-loader itself. Man it would be great if ng cli offered a way to extend the webpack conf out of the box. Like Vue does.
Reacted by Constantin Chirila
Reacted by Mick Lawitzke, Sandor Drieënhuizen, Cesar Gutierrez, LousyJohnny and Jon Clark
Guys this is how sass/scss works and has nothing todo with Angular. Just import your variables and you are good to go OR use a global stylesheets file.
Reacted by Sandor Drieënhuizen
Reacted by Constantin Chirila, Victor S., Nicholas Davis, Victor Grados, Verdi, Johnathan and Clipota Alexandru
So, in conclusion, if you @import something but you doesn't use a var or mixing of that thing you've imported, it isn't included in the final bundle, right?
Is there a way to use sass variables globally without the need to manually import in every single component? Perhaps this would be bad practice anyway as every component then has access, even if t doesn't need it...
Do one thing. It will work.
import variable.scss file in the component where you want to use variables, it will work.
This is not standard, if something standard arrives, it is easily undo-able.
Activity
filipesilva commentedon Jul 4, 2016
There isn't any such functionality in the CLI itself, no. There is sass compilation but that's it.
AmarildoK commentedon Jul 15, 2016
@a5hik A temp fix for that is to make a _varaibles.scss and import where ever you use your variables. Not the cleanest way.
pramttl commentedon Jul 16, 2016
What @AmarildoK suggested could actually be incorporated as a feature into angular CLI. There could be a shared scss file somewhere and new
.scssfiles in generated components could use have an import line to include the shared scss file by default.purushottamjha commentedon Oct 2, 2016
Yup. Certainly need this feature.
MateuszG commentedon Nov 18, 2016
Still nothing?
mhmo91 commentedon Nov 20, 2016
Any update about this people?
pquarme commentedon Dec 1, 2016
Any update on this?
jammerware commentedon Dec 1, 2016
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.scssfile.styles.scss
_vars.scss
When I try to reference a variable declared this way in a component's
.scssfile, 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.
Switched to Sass from CSS. Broke everything. Discussion active here: a…
ahmed-musallam commentedon Jan 17, 2017
can someone explain to me how the SASS transpiling happens?
I assume this happens:
is that correct?
greenchapter commentedon Jan 18, 2017
I have the same problem with global sass variables. Also the transpile is for me at the moment a blackbox
origin1tech commentedon Feb 3, 2017
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.
Forgot you also on the top level component need to set View Encapsulation.
ahmed-musallam commentedon Feb 3, 2017
@greenchapter yeah.. I wish there was some good documentation for it. if you find anything, do let me know.
23 remaining items
amirtoole commentedon Mar 27, 2018
@daniel-seitz perhaps a bit of confusion -- this issue is going over many different topics.
Using
@import “~variables.scss”;is great if your scss is inside of a folder withinnode_modules(unmodifiable).Adding
scsspaths intoangular.cli.jsonis required if you want "simple" imports for scss that are defined in your project.Without adding the appropriate scss path(s) into
angular.cli.jsonyou will have to do relative path imports.For example:
@import "./src/assets/private/sass/base";should be simplified into@import "base";(assuming you've added the correct entry into .angular.cli.json)
As for automagically importing
variablesorbaseinto all scss files, that may be a nice feature.It could be done similar to this comment:
webpack/sass-loader#218 (comment)
I do think if this feature was added it would need to be a new property of
.angular.cli.json.daniel-seitz commentedon Apr 18, 2018
Thank you for the reply @hellofornow. I too would welcome this feature however from my current understanding its not trivial to implement this.
I was not clear enough with my question for you, so one clarification:
Look out for the ~ symbol.If one just imports like this
@import “variables.scss”;, then yes this file needs to be inside node_modules - which is bad, don't do this.BUT! if you do
@import “~variables.scss”;, so with a~, then you can import relative to your src folder. That's it, no other config. (Since we're in the angular-cli repo here I won't go into other structures and what we need to configure that 'home'-path)Keeping that in mind, what's the need to do this then? I found no documentation.
Then I also did some tests (also with lazy loaded modules, ...) and - as we all know - we would not expect this to happen: Angular CLI with Sass compilation encapsulates the styles so that they won't have effects on other parts of the system.
When using import, we circumvent this and can use all variables, however when mixing variables with real css we also can cascade.
PROBLEM here is that each time use import, that 'real css' gets into the resulting file inline, over and over - which again goes against cascading and worse, makes our dist bigger.
The SOLUTION would be to make the variables (mixins, ...) available to the compilation so that it just knows about them and translating on the fly, nothing else.
I'd like to have these variables, or at least the starting point, in src/styles.scss as it is default but likely needs to be configurable.
If I find the time, I will dig deeper as I know that it's not just us who want this...
IgorceJovanoski commentedon May 8, 2018
Am I wrong or AlexHenkel's comment on Jan 23 solves the problem ?
I have my _variables.scss defined
$primary:rgb(211, 14, 127) !default;So then I can use this variable in a component
To clarify, my styles.scss looks like:
not sure if OP had same problem but works for me, cause I don't have to hard code the default color
thescientist13 commentedon Sep 4, 2018
Sorry if this is off topic, but per my understanding in this thread, it seems that SASS process files more in isolation, then say LESS did, would that be correct?
In essence, the use case is of being able to use a
@mixinwhich sets some color variables in one file at an "app" level, and hope for that cascade down into all the other components (even if node_modules).variables.css
app.component.css
header.component.css
In this case, anything processed in my app component using
$colorwould change red, but anything in my other components would not change color (would stay the default color of green) unless I manually call setTheme in all my components, which doesn't make them very reusable.Thanks, and hope that wasn't too confusing! Any thoughts would be greatly appreciated!
thescientist13 commentedon Sep 4, 2018
I was able to get something working for my use case using sass-vars-loader.
peterankelein commentedon Oct 4, 2018
Just an FYI, it looks like using the tilde to reference 'src' from the component level ala
@import '~scss/variables';is buggy (not working) in Angular 6 (issue).@import '~src/scss/variables';and@import './src/scss/variables';seem to work, though. Just a PSA if anyone runs into problems.rdalfonso commentedon Feb 16, 2019
I can't believe how difficult Angular makes using SASS. I'm about to switch to React/webpack
ManuelGraf commentedon Feb 27, 2019
this is ONE OF THE MOST COMMON THINGS I CAN IMAGINE. How can you get that wrong?!
also: you can make this work easily via webpack and sass-loader itself. Man it would be great if ng cli offered a way to extend the webpack conf out of the box. Like Vue does.
MickL commentedon Feb 27, 2019
Guys this is how sass/scss works and has nothing todo with Angular. Just import your variables and you are good to go OR use a global stylesheets file.
futbolsalas15 commentedon Mar 29, 2019
So, in conclusion, if you
@importsomething but you doesn't use a var or mixing of that thing you've imported, it isn't included in the final bundle, right?cmacdonnacha commentedon Apr 15, 2019
Is there a way to use sass variables globally without the need to manually import in every single component? Perhaps this would be bad practice anyway as every component then has access, even if t doesn't need it...
nikhilknoldus commentedon Jun 21, 2019
Do one thing. It will work.
import
variable.scssfile in the component where you want to use variables, it will work.This is not standard, if something standard arrives, it is easily undo-able.
batousik commentedon Sep 12, 2019
What if i want separate css bundles, that i can switch at runtime, each bundle using its own _variables.scss?
angular-automatic-lock-bot commentedon Oct 13, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.