手动导入方案
1,准备样式变量文件
// 主题
@xtxColor:#27BA9B;
// 辅助
@helpColor:#E26237;
// 成功
@sucColor:#1DC779;
// 警告
@warnColor:#FFB302;
// 价格
@priceColor:#CF4444;
2手动引入其中的变量
<style scoped lang='less'>
@import '~@/styles/variables.less';
.xtx-home-page{
color: @xtxColor;
}
</style>
自动引入方案
使用vue-cli的style-resoures-loader插件来完成自动注入到每个vue组件中style标签中
1,在当前项目下执行一下命令vue add style-resources-loader
,添加一个vue-cli的插件
2, 安装完毕后会在vue.config.js
中自动添加配置,如下:
module.exports = {
pluginOptions: {
'style-resources-loader': {
preProcessor: 'less',
patterns: []
}
}
}
3,把需要注入的文件配置一下后,重启服务即可
const path = require('path')
module.exports = {
pluginOptions: {
'style-resources-loader': {
preProcessor: 'less',
patterns: [
// 配置哪些文件需要自动导入
path.join(__dirname, './src/styles/variables.less')
]
}
}
}