Reputation: 133
I created a vue.config.js file to set some global sass variables (just like the documentation specifies), however when trying to access the variables in a component, I get an undefined error. Adding the same import statement manually in the component works, but somehow it's not being picked up from inside the vue.config.js file. I checked that I have node-sass and sass-loader installed and that the vue.config.js is in the project root (next to the package.json). What am I missing?
module.exports = {
css: {
loaderOptions: {
sass: {
data: `
@import "@/assets/styles/_variables.scss";
`
}
}
}
}
Upvotes: 5
Views: 19204
Reputation: 5071
Change data
to prependData
module.exports = {
css: {
loaderOptions: {
sass: {
prependData: `
@import "@/assets/styles/_variables.scss";
`
}
}
}
}
You can read more about this here: pre-loader docs
Upvotes: 6
Reputation: 111
this code is for vue3
install:
npm install -D sass-loader@^10 sass
npm install -D fibers
npm i style-resources-loader -D
in your vue.config.js (root level project)
module.exports = {
css: {
loaderOptions: {
sass: {
additionalData: `
@import "@/assets/styles/scss/_variables.scss";
`
}
}
}
}enter image description here
symbol @
means that your file start from src folder
Upvotes: 6
Reputation: 89
npm install --save-dev node-sass sass-loader
Add code below. Remember - @ refers to /src folder
module.exports = {
css: {
loaderOptions: {
sass: {
data: '@import "@/assets/css/variables/_colors.scss";'
}
}
}
}
Restart the project
Upvotes: 3
Reputation: 520
It's hard to troubleshoot without the full context, so here are several options you can try:
Ensure you've restarted the dev environment since changing the config (re-run yarn dev
or npm run dev
)
Keep the template literal to one line, as is used in docs. This shouldn't make a difference, but it might. (e.g. data: `@import "@/assets/styles/_variables.scss";`
)
As you probably know, the underscore in front of a sass file denotes a sass partial. A partial is not used in the example, so it is possible that this has an effect as well. (e.g. rename _variables.scss
to variables.scss
and use data: `@import "@/assets/styles/variables.scss";`
)
Ensure that the sass-loader
, node-sass
, and css-loader
packages are up to date.
Try using the path without the slash after the @
. e.g. @assets/styles/_variables.scss
.
Try with a ~
instead of the @
. e.g. ~assets/styles/_variables.scss
. If nothing else has worked, try replacing the @
with src
as well.
Good luck!
Upvotes: 1