less配置全局公用变量文件,如何以变量的形式在html中 引入

一、在vue.config.js文件中

const path = require('path')

const vars = path.resolve(__dirname, './src/assets/css/common.less') 

module.exports = {
    css: {
        loaderOptions: {
            less: {
                globalVars: {
                    hack: `true; @import "${vars}"`,
                },
            },
        },
    },
}

 二、以变量的形式引入

1、在公共的less文件中导出

@menuBg:red;

:export {
    menuBg: @menuBg;
}

2.1、在man.js全局引入

import commonColor from './assets/css/common.less'

Vue.prototype.commonColor = commonColor

3、使用

//进度条自定义颜色
<el-progress :percentage="20" :color="commonColor.menuBg" />

4、局部引入(使用同上)

import commonColor from '@/assets/css/common.less'

//计算属性
 computed: {
        commonColor() {
            return commonColor
        },
  },
原文地址:https://www.cnblogs.com/zhangjianhua26/p/15035338.html