vue-antd动态切换主题

安装依赖

1 webpack-theme-color-replacer: yarn add webpack-theme-color-replacer@1.3.22
2 less: yarn add less@2.7.2
3 less-loader: yarn add less-loader@7.0.2

在vue.config.js中加入配置

const createThemeColorReplacerPlugin = require("./plugin.config");
const vueConfig = {
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          modifyVars: { 
            //注意:此处不能给默认值,否则会导致主题色动态配置失败
            // less vars,customize ant design theme
            //'primary-color': '#2f54eb',
            //'link-color': '#2f54eb',
            //'border-radius-base': '4px'
          },
          // DO NOT REMOVE THIS LINE
          javascriptEnabled: true,
        },
      },
    },
  },
  configureWebpack: {
    plugins: [createThemeColorReplacerPlugin()],
  },
  assetsDir: "static",
};
module.exports = vueConfig;

添加主题色更改方法,新建util文件夹创建plugin.config.js

const ThemeColorReplacer = require('webpack-theme-color-replacer')
const generate = require('@ant-design/colors/lib/generate').default

const getAntdSerials = (color) => {
  // 淡化(即less的tint)
  const lightens = new Array(9).fill().map((t, i) => {
    return ThemeColorReplacer.varyColor.lighten(color, i / 10)
  })
  const colorPalettes = generate(color)
  const rgb = ThemeColorReplacer.varyColor.toNum3(color.replace('#', '')).join(',')
  return lightens.concat(colorPalettes).concat(rgb)
}

const themePluginOption = {
  fileName: 'css/theme-colors-[contenthash:8].css',
  matchColors: getAntdSerials('#1890ff'), // 主色系列
  // 改变样式选择器,解决样式覆盖问题
  changeSelector (selector) {
    switch (selector) {
      case '.ant-calendar-today .ant-calendar-date':
        return ':not(.ant-calendar-selected-date):not(.ant-calendar-selected-day)'   selector
      case '.ant-btn:focus,.ant-btn:hover':
        return '.ant-btn:focus:not(.ant-btn-primary):not(.ant-btn-danger),.ant-btn:hover:not(.ant-btn-primary):not(.ant-btn-danger)'
      case '.ant-btn.active,.ant-btn:active':
        return '.ant-btn.active:not(.ant-btn-primary):not(.ant-btn-danger),.ant-btn:active:not(.ant-btn-primary):not(.ant-btn-danger)'
      case '.ant-steps-item-process .ant-steps-item-icon > .ant-steps-icon':
      case '.ant-steps-item-process .ant-steps-item-icon>.ant-steps-icon':
        return ':not(.ant-steps-item-process)'   selector
      case '.ant-menu-horizontal>.ant-menu-item-active,.ant-menu-horizontal>.ant-menu-item-open,.ant-menu-horizontal>.ant-menu-item-selected,.ant-menu-horizontal>.ant-menu-item:hover,.ant-menu-horizontal>.ant-menu-submenu-active,.ant-menu-horizontal>.ant-menu-submenu-open,.ant-menu-horizontal>.ant-menu-submenu-selected,.ant-menu-horizontal>.ant-menu-submenu:hover':
      case '.ant-menu-horizontal > .ant-menu-item-active,.ant-menu-horizontal > .ant-menu-item-open,.ant-menu-horizontal > .ant-menu-item-selected,.ant-menu-horizontal > .ant-menu-item:hover,.ant-menu-horizontal > .ant-menu-submenu-active,.ant-menu-horizontal > .ant-menu-submenu-open,.ant-menu-horizontal > .ant-menu-submenu-selected,.ant-menu-horizontal > .ant-menu-submenu:hover':
        return '.ant-menu-horizontal > .ant-menu-item-active,.ant-menu-horizontal > .ant-menu-item-open,.ant-menu-horizontal > .ant-menu-item-selected,.ant-menu-horizontal:not(.ant-menu-dark) > .ant-menu-item:hover,.ant-menu-horizontal > .ant-menu-submenu-active,.ant-menu-horizontal > .ant-menu-submenu-open,.ant-menu-horizontal:not(.ant-menu-dark) > .ant-menu-submenu-selected,.ant-menu-horizontal:not(.ant-menu-dark) > .ant-menu-submenu:hover'
      case '.ant-menu-horizontal > .ant-menu-item-selected > a':
      case '.ant-menu-horizontal>.ant-menu-item-selected>a':
        return '.ant-menu-horizontal:not(ant-menu-light):not(.ant-menu-dark) > .ant-menu-item-selected > a'
      case '.ant-menu-horizontal > .ant-menu-item > a:hover':
      case '.ant-menu-horizontal>.ant-menu-item>a:hover':
        return '.ant-menu-horizontal:not(ant-menu-light):not(.ant-menu-dark) > .ant-menu-item > a:hover'
      default :
        return selector
    }
  }
}

const createThemeColorReplacerPlugin = () => new ThemeColorReplacer(themePluginOption)

module.exports = createThemeColorReplacerPlugin

添加util.js文件

import client from "webpack-theme-color-replacer/client";
import generate from "@ant-design/colors/lib/generate";

function getAntdSerials(color) {
  // 淡化(即less的tint)
  const lightens = new Array(9).fill().map((t, i) => {
    return client.varyColor.lighten(color, i / 10);
  });
  // colorPalette变换得到颜色值
  const colorPalettes = generate(color);
  const rgb = client.varyColor.toNum3(color.replace("#", "")).join(",");
  return lightens.concat(colorPalettes).concat(rgb);
}
function changeColor(newColor) {
  var options = {
    newColors: getAntdSerials(newColor), // new colors array, one-to-one corresponde with `matchColors`
    changeUrl(cssUrl) {
      return `/${cssUrl}`; // while router is not `hash` mode, it needs absolute path
    },
  };
  return client.changer.changeColor(options, Promise);
}

export default {
  updateTheme(newPrimaryColor) {
    const hideMessage = () => console.log("正在切换主题!", 0);
    changeColor(newPrimaryColor).finally((t) => {
      setTimeout(() => {
        hideMessage();
      });
    });
  },
};

然后在main.js中引入为Vue原型prototype中的方法

import utils from "./utils"
Vue.prototype.$u = utils
//注意antd样式的引入必须由css改为less引入
- import 'ant-design-vue/dist/antd.css';
  import 'ant-design-vue/dist/antd.less';

然后项目启动起来后调用Vm.$u.updateTheme方法即可动态改变主题色。

注意该方法传值必须传十六进制的颜色,如果传颜色对应的英文字符可能会出错.

如果自定义组件或者元素也需要统一管理主题色,则在assets目录新建main.less

@import "~ant-design-vue/dist/antd.less";
#home {
  color: @primary-color;
}

在main.less中引入antd的样式,然后antd的主题色是@primary-color, 直接使用即可

然后在main.js中直接引入main.less

- import 'ant-design-vue/dist/antd.less';
  import "./assets/css/main.less"

Vue3.0 Antd,修改antd主题色,配置全局css

1.在vue.config.js里修改配置

module.exports = {
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          modifyVars: {
            'primary-color': '#3d62ff',// 修改全局主题色
          },
          javascriptEnabled: true,
        },
      },
    },
    extract: true, // 解决开发模式,打包时未提取CSS的问题
  }
}

2.把main.ts中的import ‘ant-design-vue/dist/antd.css’;

修改为 import ‘ant-design-vue/dist/antd.less’;

修改css为less的时候,会报错 .bezierEasingMixin()

解决方法是:先卸载less-loader

npm uninstall -D less-loader

再安装less-loader@6.0.0

npm install -D less-loader@6.0.0

然后重新运行项目即可 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持Devmax。 

使用vue-antd动态切换主题的更多相关文章

  1. jquery实现网站列表切换效果的2种方法

    这篇文章主要为大家详细介绍了jquery实现网站列表切换效果的2种方法,供大家参考,感兴趣的小伙伴们可以参考一下

  2. 使用AngularJS2中的指令实现按钮的切换效果

    这篇文章主要介绍了使用AngularJS2中的指令实现按钮的切换效果,非常不错,具有参考借鉴价值,需要的朋友可以参考下

  3. Android实现图片自动切换功能(实例代码详解)

    这篇文章主要介绍了Android实现图片自动切换功能,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

  4. 广告切换效果(缓动切换)

    写了一个 广告切换效果(点击查看DEMO) 形式是常见的形式,但是在功能上有一定的拓展。

  5. JS实现简单Tab栏切换案例

    这篇文章主要为大家详细介绍了JS实现简单Tab栏切换案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  6. jQuery实现切换隐藏与显示同时切换图标功能

    这篇文章主要介绍了jQuery实现切换隐藏与显示同时切换图标功能,需要的朋友可以参考下

  7. mac下多个php版本快速切换的方法

    这篇文章主要给大家介绍了在mac下多个php版本快速切换的方法,本文先是给大家分享了使用brew安装php多版本的方法,而后才介绍了快速切换的实现方法,感兴趣的朋友们下面来一起看看吧。

  8. Android实现底部状态栏切换的两种方式

    这篇文章主要介绍了Android实现底部状态栏切换功能,在文中给大家提到了两种实现方式,本文分步骤给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

  9. iOS自定义键盘切换效果

    这篇文章主要为大家详细介绍了iOS自定义键盘切换效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. jquery实现点击左右按钮切换图片

    这篇文章主要为大家详细介绍了jquery实现点击左右按钮切换图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部