angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Can't access Sass Global Variables in components scss

Open paramsingh88 opened this issue 9 years ago • 29 comments

I have a habit of importing my _variables.scss and _mixins.scss into my root src/style.scss file

@import 'styles/base/_variables.scss';
@import 'styles/base/_mixins.scss';
@import 'styles/helpers/flexbox.scss';
* {
    padding: 0;
    margin: 0;
}

body{
	padding: 4%;
	font-family: sans-serif;
}

But 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.

paramsingh88 avatar Dec 22 '16 09:12 paramsingh88

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.

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.

ManuelGraf avatar Feb 27 '19 16:02 ManuelGraf

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.

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.

grizzm0 avatar Feb 27 '19 16:02 grizzm0

Does that also hold for mixins and placeholders? I actually had the same issue with Vue CLI and fxed it by using webpack loader oprions.

    loaderOptions: {
      sass: {
        data: `
          @import "@/assets/styles/vars.scss";
          @import "@/assets/styles/mixins.scss";
        `
      }
    }

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 avatar Feb 27 '19 16:02 ManuelGraf

@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.

dewwwald avatar Feb 27 '19 17:02 dewwwald

@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.

grizzm0 avatar Feb 27 '19 17:02 grizzm0

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 avatar Feb 27 '19 17:02 ManuelGraf

@ManuelGraf try this: @import ~src/variables in component.scss.

sandgupta23 avatar Mar 08 '19 18:03 sandgupta23

This topic has been over explained, no one reads the thread before posting. Could we lock the topic?

dewwwald avatar Mar 08 '19 18:03 dewwwald

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.