less使用手记 主题切换 全局import less

实现主题颜色切换

components/theme.less,跟据@theme读取主题布局
@theme: dark;

.dark-theme (@transparency) when (@theme = default) {
  color: rgba(0, 0, 0, @transparency);
}

.dark-theme (@transparency) when (@theme = dark) {
  color: rgba(255, 255, 255, @transparency);
}

.dark-theme-color (@transparency) when (@theme = default) {
  color: rgba(0, 0, 0, @transparency);
}

.dark-theme-color (@transparency) when (@theme = dark) {
  color: rgba(255, 255, 255, @transparency);
}

  

引用theme.less

<style lang="less" scoped>
  @import '~@comp/theme.less';
  .title {
    .dark-theme-color(0.85);
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 16px;
  }
</style>

  

需先在vue.config.js做配置

chainWebpack: (config) => {
    config.resolve.alias
      .set('@$', resolve('src'))
      .set('@api', resolve('src/api'))
      .set('@assets', resolve('src/assets'))
      .set('@comp', resolve('src/components'))
      .set('@views', resolve('src/views'))
      .set('@layout', resolve('src/layout'))
      .set('@static', resolve('src/static'))

  

这样就可以实现主题化

------------------------------分割线--------------------------------------

这里有一个问题,就是有用到theme.less都要引入一次,很不方便

接下来实现全局加载theme.less

首先用npm或者yarn这三个

vue-cli-plugin-style-resources-loader
style-resources-loader
sass-resources-loader(前面两个装完不生效的话,可以尝试装它)

 然后在vue.config.js新增pluginOptions配置

pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'less',
      patterns: [path.resolve(__dirname, 'src/assets/css/theme.less')]
    }
}

最后将theme.less移到src/assets/css/重新 run 一下就可以了

 

后面会讲如何通过点击切换主题

https://segmentfault.com/a/1190000016047076

https://blog.csdn.net/u013884068/article/details/78186798

https://blog.csdn.net/LuckyMon/article/details/89923822

https://segmentfault.com/a/1190000016061608

先备份一下参考资料

转载于:https://www.cnblogs.com/cxscode/p/11260319.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值