一.引入node-sass
和sass-loader
依赖
javascript
npm install --save-dev sass-loader node-sass
二. 在项目根目录中创建一个scss
变量文件,比如constant.scss
:
三.在vue.config.js
文件中引入这个变量文件:
javascript
// vue.config.js
module.exports = {
css: {
loaderOptions: {
sass: {
prependData: `@import "@/utils/css/constant.scss";`
}
}
}
};
四.版本错误的配置
sass-loader版本问题引发的错误:options has an unknown property 'prependData'. These properties are valid
当前版本如下:
"sass": "^1.32.7",
"sass-loader": "^12.0.0",
原来的写法:
javascript
css: {
loaderOptions: {
sass: {
prependData: '@import "@/utils/css/constant.scss";'
}
}
}
修改后(生效):
javascript
module.exports = defineConfig({
transpileDependencies: true,
lintOnSave: false,
css: {
loaderOptions: {
sass: {
additionalData: '@import "@/utils/css/constant.scss";'
}
},
}
})
多个文件全局配置
javascript
css: {
loaderOptions: {
scss: {
additionalData: `
@import "@/style/utils/_variables.scss";
@import "@/style/utils/mixin.scss";
`
}
}
}