位置: IT常识 - 正文

使用vue-antd动态切换主题(vue 动态tab)

编辑:rootadmin
这篇文章主要介绍了使用vue-antd动态切换主题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教 目录

推荐整理分享使用vue-antd动态切换主题(vue 动态tab),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:ant design vue 动态菜单,antd-vue-pro,vue中如何实现动态组件,ant design vue 动态菜单,antd vue pro 动态路由,antd vue pro 动态路由,antd vue pro 动态路由,antd vue pro 动态路由,内容如对您有帮助,希望把文章链接给更多的朋友!

vue-antd动态切换主题安装依赖Vue3.0 + Antd,修改antd主题色,配置全局cssvue-antd动态切换主题安装依赖1 webpack-theme-color-replacer: yarn add webpack-theme-color-replacer@1.3.222 less: yarn add less@2.7.23 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').defaultconst 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方法即可动态改变主题色。

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

使用vue-antd动态切换主题(vue 动态tab)

如果自定义组件或者元素也需要统一管理主题色,则在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

然后重新运行项目即可

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

本文链接地址:https://www.jiuchutong.com/zhishi/304790.html 转载请保留说明!

上一篇:phpcms上传不了图片怎么办(php实现上传图片功能)

下一篇:一起爬山吗?《隐秘的角落》为什么译成Cat's Cradle?(你要和我一起爬山吗)

  • 小规模增值税纳税申报
  • 公司之间往来款需要开收据吗
  • 公司公积金缴纳比例一般来说是多少?
  • 开票打印机可以自己买吗
  • 核定征收率怎么算的
  • 增值税抵扣不够怎么解决
  • 总分机构汇算清缴成功后还需要填表什么报表
  • 分期收款销售货物 收入确认
  • 处置全资子公司税务处理
  • 一般销售商品业务
  • 公司购买的冰箱供员工使用
  • 出售房产收入计入什么科目
  • 加工费的增值税税率是多少
  • 公司的钱怎么提现
  • 分公司可以单独签协议吗
  • 三证合一后未办理税务登记
  • 企业报税的详细流程期限为
  • 企业所得税法如何确认应税收入
  • 什么是应收
  • windows10如何开机启动软件
  • win11如何更改开始菜单位置
  • 出售无形资产是什么科目
  • 应付职工薪酬计提数是借方还是贷方
  • mac如何打印预览
  • 商会收到的会费要交企业所得税
  • 借别人资质投标如何签合同
  • 单位银行结算账户属于活期存款账户
  • 受托代销商品会计科目
  • 邮政电信卡
  • php imagettftext()函数
  • 餐饮发票可以计入福利费吗
  • 数组 php
  • openlayers6教程
  • 入库税款异常怎么处理
  • 织梦使用教程
  • 退股东股本账务处理
  • 三免三减半递延所得税案例
  • 商品流通企业的种类
  • 无形资产是有在期资产吗
  • 企业所得税会计利润
  • 详解中国女足出线形势
  • Sqlserver 2005使用XML一次更新多条记录的方法
  • 使用XQuery查询DB2 XML数据
  • mysql两个数据库连接查询
  • 认缴出资额就是营业执照上的注册资金
  • 企业所得税汇算清缴操作流程
  • 进项大于销项月末怎么处理
  • 销售商品的折扣
  • 律师事务所日语助理
  • 物流公司挂靠车辆如何做账?
  • 工程费用科目
  • 投资者以现金支出为准
  • 分公司可以设立公司吗
  • 固定资产帐怎么做
  • mysql5.7免安装版
  • windows server 2008 r2激活密钥
  • linux终端记录
  • win7鼠标右键没有压缩文件
  • windows如何禁用程序
  • win7桌面没有了怎么办
  • window mobile系统
  • linux rm 命令删除文件恢复
  • js入门基础
  • node js教程
  • bat批处理命令大全
  • html5的全局属性
  • 深入理解python特性pdf百度云
  • Python中利用不同Excel表的列匹配
  • unity游戏开发入门经典
  • JavaScript驾驭网页-获取网页元素
  • unity游戏开发简历
  • Python 安装模块
  • unity-gain
  • js的类型有哪几种
  • python编写一个模块
  • android数据存储与访问的方式有
  • 税务稽查工作底稿属于什么证据
  • 非营利组织认定条件
  • 耕地占用税 湖北
  • 企业所得税地方留存比例2023
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设