Can't access global SASS variables from my component

can definition
can synonym
can verb
can y
can noun meaning
can band
cant
can't

In my Nuxt app I load all my SASS thus:

css: [
  '~assets/scss/main.scss'
],

It works perfectly, except when I'm trying to use some SASS variable from within a component.

<style lang="scss">
.container {
    background-color: $white;
}
</style>

In this case I get this error message:

SassError: Undefinied variable: $white

Yet, all of the SCSS contained in the SASS file where the variable is defined works throughout the app.

It is as if the app as a whole knew these files, but each individual component doesn't.

What's going on?

Most of the other answers don't take into account that Nuxt.js hides all the Webpack setup and forces you to do everything through nuxt.config.js.

My guess is that Webpack isn't compiling all the SCSS declarations together and therefore can't find the variable.

It's been a few months since I had this issue so things may have changed but here goes...

  1. Make sure you have the correct Node packages installed (Nuxt DID NOT do this by default for me) npm i -D node-sass sass-loader
  2. Add your CSS & SCSS files to the css: [] section of nuxt.config.js Order matters here so make sure things like variables are added before things that use them if you have separate files.
  3. If you're using layouts (I think that's the default Nuxt setup) make sure that layouts/default.vue has a <style lang="sass"></style> block in it. If I remember correctly this can be empty but had to exist. I only have one layout but it may need to exist in all of them.

If all that seems like too much of a pain, there's a Nuxt Plugin that takes most of the work/management out of that process. Nuxt Style Resources Module

Can, Can may refer to: Contents. 1 Containers; 2 Music; 3 Other; 4 See also. Containers[edit]. Aluminum can · Drink can · Oil can · Steel and tin cans · Trash can  Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.

Ref:

SassError: Undefinied variable: $white

Each <style lang="scss"> is compiled individually. You need to @import the file which defines $white into your component before the parser knows what $white means.

This is why most frameworks keep their variables in a _variables.scss file which is imported in all the other SCSS files/contexts.

The _variables.scss is not even loaded in the page, because in most cases it doesn't actually contain any rules. It only contains variable definitions which are imported into other .scss files, which output .css.


Ref:

Yet, all of the SCSS contained in the SASS file where the variable is defined works throughout the app.

If you import an SCSS file in your vue.config.js the output will be an ordinary <style> tag. Its contents will be generated at compile/build time and will result into some CSS (which apply to the entire document). Unless specifically imported into the component SCSS, (using an @import command), the compiler will not know what $white means.

There is an important distinction to make between compilation context and browser context. Compilation happens at compile time (most likely in node-sass). Browser context is the actual browser, which only understands the CSS resulted from compilation.


How does Vue only apply style rules to the parent and not to the children with the same class? That's achieved by scoping.

It means applying a custom data-v-{key} attribute to all selectors in the generated <style> tag and to all elements the style should apply to.

See this example and inspect it using your web console: https://codesandbox.io/s/vue-template-ge2hb

It produces this markup:

As you can see, the scoped CSS has an extra [data-v-763db97b] added to the selector, which means it only applies to elements having that data attribute.

The Official CAN / Spoon Records Website, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the

The confusing part is that:

styles from scss files CAN be loaded like this

//nuxt.config.js
css: [
  '~assets/scss/main.scss'
],

//global scss file
$varcolor: black
h1{background: $varcolor}

BUT

the variables inside CAN NOT be used inside a component

//inside component
.component {background: $varcolor} // DOES NOT WORK

I also suggest the use of the nuxt style resource module: https://github.com/nuxt-community/style-resources-module

Can, can meaning: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more. can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more.

Us should either load the scss in your component

<style lang="sass">
@import 'path/to/your/_variable.scss'; // Using this should get you the variables

 .my-color {
      color: $primary-color;
 }

Or adding the following to you to your vue.config.js

module.exports = {
  css: {
    loaderOptions: {
      sass: {
        data: `@import "@/pathto/variables.scss";`
      }
    }
  }
};

can, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.

CAN, can. (kæn). n. 1. a container, esp for liquids, usually of thin sheet metal: a petrol can;  Can means someone or something knows how to, is able to, is likely to or has the right to do something. An example of can is someone knowing how to play the piano. An example of can is a cat being able to paint. An example of can is a car that usually starts.

Can, A can is a metal container in which something such as food, drink, or paint is put. The container is usually sealed to keep the contents fresh. Several young men  Looking for online definition of CAN or what CAN stands for? CAN is listed in the World's largest and most authoritative dictionary database of abbreviations and acronyms The Free Dictionary

Can, Can-Am offers a complete lineup of Off-Road vehicles (ATV, Side-by-Side) and 3-​wheel motorcycles (Spyder or Ryker). Choose your playground now and enjoy  Inflections of 'can' (v): (⇒ conjugate) can v aux present could v aux past could v aux conditional cans v 3rd person singular (For the transitive verb) canning v pres p verb, present participle: -ing verb used descriptively or to form progressive verb--for example, "a singing bird," "It is singing."

Comments
  • I ended up using Style Resources Module and not it works fine, thanks!