-
Notifications
You must be signed in to change notification settings - Fork 12k
Can't access Sass Global Variables in components scss #3700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You need to include |
Seriously there is no better way for this? |
This is how SCSS has always worked. It really has nothing to do with the Angular CLI. The component's CSS is being compiled by itself - the other files aren't there. There's no reason for them to be there. This is not only how it works, this is how you should WANT it to work. You don't want coupling with monolithic styles. You WANT each component to be a COMPONENT. They are isolated, encapsulated pieces of logic. |
Add this to .angular-cli.sjon (change includePath to your path):
|
@JustasKuizinas if you want you can:
More on this here: As mentioned above this is not an angular issue but more of a seed (project base that use angular) issue (with all the build processes and pre-processing pre-dictated by the author). |
Looks like we just need to duplicate in every component. @import "~style/variables"; |
@AlexanderKozhevin Exactly. Just like you need to import anything you use in typescript you also need to import anything you use in SCSS. |
I can make another recommendation on this. I am buzzy building a framework that will have no classes, only placeholders, mixins and variables. You can import this in the tag and write your sass as per usual but for the component. My afore mentioned solution has some problems, for example css has not loops and ifs. Therefore it is better to just use sass. You can then import base styles like import grid or responsive-grid in you app component (root component). |
Well, there is this loader that could resolve all our problems - https://github.com/shakacode/sass-resources-loader |
@GuskiS Yep, sass resources loader is great, but out of the box ng cli karma config will fail with it. SASS vars will not get picked up, but during a build and development it will work. ...Currently trying to figure out to get my tests to pass with it 🤔 |
@GuskiS that looks like the best option, combining that with importing sass modules using angular's Again, as always, setup usually depends on your project. |
My current solution is to turn off ViewEncapsulation for the component, but I dont like this at all. I wish there was a clean solution for this. @component({ |
@aoakeson, form my understanding, that is based on experience and not the actual algorithm, view encapsulation only applies when styling is applied to a class specific to that component. I haven't had to turn on viewEncapsulation for any of my component. The global stylesheets just seem to apply. I am curious to know why this is different for you. |
@dewwwald I think I figured out my problem. It turns out the elements that I was trying to access using these global variables were outside the scope of the main angular app. As my app is comprised of multiple modules, and apps it was not able to access these. I moved things around and am now able to have my components keep ViewEncapsulation on. |
You should never do this because #8431 :( Thus component.scss is totally unusable in real projects now... |
@LastDragon-ru As explained in that issue you should never import files that actually render CSS more than once. You can import files that contain variables and mixins as many times as you want as they do not affect the output. |
@grizzm0, For example with bootstrap sometimes more simply and better to use something like |
I prefer creating |
@kkuzmina I know that it`s not right thing to do, but helped me as well 👍 |
how does importing a static path fit to that cool isolation idea of yours? I call BS. Duplication of declarations is what really sucks. Thats why there are things like tree-shaking and module systems. But its not like i can create style modules (singletons) in angular which I can inject somewhere. |
If you need to use a variable you need to import the file the variable is declared in. A variables file does not produce any output while imported and wont affect the build size. For the variables to work application wide angular would have to include your global application SCSS in each component SCSS which would produce duplicated CSS. If you import the variables file where you need it each component will have it's own "isolated" CSS while it actually "borrows" variables from a shared SCSS file. Just as you would do with a shared module in typescript/angular. This way there's no need to declare the variable multiple times. But if you want to you could skip the import and redeclare the same variable over and over again, which seems a bit annoying to me. |
Does that also hold for mixins and placeholders?
I personally don't like scoped css (which Vue OFFERS and doesnt REQUIRE), I also don't really care that i have to import my mixins, vars, placeholders etc. I just want to be sure none of those will be printed (multiple times) in my final output. |
@ManuelGraf only css classes, id's and statements get printed in the final result. Mixins are reuse-able statements that print nothing until they are used. Same holds for placeholders, mixins and scss vars. |
@ManuelGraf Variables nor mixins produces any output while imported. Comments in those files will (the last time I checked) however be printed multiple times. But the CLI should strip all comments when building anyhow. |
Okay I am going to use it as proposed. But for some reason the ~/... doesnt work for me. What is ~ exactly in a multi-project angular 7 CLI environment? |
@ManuelGraf try this: |
This topic has been over explained, no one reads the thread before posting. Could we lock the topic? |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I have a habit of importing my _variables.scss and _mixins.scss into my root
src/style.scss
fileBut when I'm trying to access the variables in my components.scss, it's giving me an error
color: $primary-color;
Isn't there a way I can achieve this simply? Please Help.
The text was updated successfully, but these errors were encountered: