less如何注册全局的属性

这篇博客介绍了在Web开发中,特别是在Vue项目中如何定义和使用Less全局属性。通过配置`vue.config.js`文件,利用`sass-resources-loader`解决非同文件中引用不到全局变量的问题,确保在多个组件中可以共用同一组样式变量,提高代码复用性和可维护性。
摘要由CSDN通过智能技术生成

在web项目的开发中less的使用比较的多
那么怎么定义less的全局属性
例如:

同文件中

/*   style/config.less */
@themeColor: #f37b1d;


/* 同文件中 */

.item.active {
   color: @themeColor;
   border-color: @themeColor;
}

非同文件中

/*  某个不知名的的vue文件 */
.item.active {
   color: @themeColor;
   border-color: @themeColor;
}

正常来说会报错

如何解决

// 配置 全局 less 加载前的变量 先下载依赖
// npm i sass-resources-loader -s


// vue.config.js
module.exports = {
  publicPath: "./",
  outputDir: 'dist',
  /* 放置生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录 */
  assetsDir: "static",
  chainWebpack: (config) => {
    // 配置 @ 路径
    config.resolve.alias
      .set('@', resolve('src'))
    // 配置 全局 less 加载前的变量
    // npm i sass-resources-loader -s
    const oneOfsMap = config.module.rule('less').oneOfs.store
    oneOfsMap.forEach(item => {
      item
        .use('sass-resources-loader')
        .loader('sass-resources-loader')
        .options({
          // Provide path to the file with resources
          resources: resolve('src') + '/style/config.less',
          // Or array of paths
          // resources: ['./path/to/vars.scss', './path/to/mixins.scss', './path/to/functions.scss']
        })
        .end()
    })
  },
}

最后

我还想补充点什么的来着

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厚渡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值