I am trying to import a scss file within my VueJS project, where it will then compile and thus be able to use with my project. However, I need some help, as at present it si
There is some solutions to this situation.
nuxt.config.js
file.It depends on what you used for creating a Vue project but when you're using the Vue CLI 3 tool you can set this up in your vue.config.js
file located in your root. This file is for config of your Vue project and is used a lot to overwrite your webpack config (that isn't there unless you eject).
npm install -D sass-loader node-sass
After that, you can add this to your vue.config.js
file
module.exports = {
css: {
loaderOptions: {
sass: {
data: `
@import "./src/main.scss";
`,
},
},
},
}
And then you can import all your scss files in your main.scss
. This way you can also use variables in your components. Personally I would not recommend to have separate stylesheets. If your project scales you will probably delete components and then end up with styles you don't use anymore. If you write your scss in the components itself you will also delete styles when you delete your component. I would go for only some global styles in a separate file and a file for your variables. Hope this helps.
I updated the style tag to include the import statement and it works.
@import 'scss/main.scss';
#app {
display:block;
}
</style>
to make it simple just use this trick
vue.config.js
(if it was not auto-created by vue-cli
when you were creating your project)module.exports = {
css: {
loaderOptions: {
sass: {
prependData: `
@import "@/assets/scss/main.scss";
`
}
}
}
};
@
means src
dir this means that @/assets
is the same as src/assets
main.scss
file, where you include all of you sub scss files if you want to know how it is made, take a look here it is really cool to work with itprependData
may not work or throw an error the reason why I used these is because I am using "sass-loader": "^8.0.2"
. here are some useful information
- on
"sass-loader": "^7.*.*"
try to usedata
- on
"sass-loader": "^8.0.2"
try to useprependData
- on
"sass-loader": "^9.0.0"
try to useadditionalData
vue.config.js
vuetify
it may throw an error, so make sure that you match the processor name with the extension, like if you have .scss
files you will need to use these configs scss: { //the change was made here (match the option name with file extension)
prependData: `
@import "@/assets/scss/main.scss";
`
}
if you are using .sass
file use these configs
sass: { //the change was made here (match the option name with file extension)
prependData: `
@import "@/assets/scss/main.sass";
`
}
I am using below Vue.config.js file configuration with sass-loader 9.0.2. And all is working fine.
NOTE: additionalData is used instead of data or prependData on the latest versions of SASS-LOADER.
module.exports = {
css: {
loaderOptions: {
sass: {
additionalData: `
@import "@/styles/main.scss";
`
}
}
}
};