当前位置: 首页 > news >正文

element-plus主题换色

提示:本篇暂未完善全,仅仅提供思路

具体的实现可以参考我这篇文章,验证可行推荐使用该链接方式实现换色

主题方式是通过切换主题的方式实现换色,例如blue、green,不推荐,仅参考逻辑
原因:
1.不好更改element-plus的默认css变量
2.对于我的项目(基于element-plus封装了公司组件库,且引入项目使用),无法将修改好的element-plus的css变量渗透到公司的组件库


文章目录

  • 一、步骤

一、步骤

1.src/style/element.scss

// index.scss@forward"element-plus/theme-chalk/src/common/var.scss"with($colors:("white":#ffffff,"black":#000000,"primary":("base":#007d7b),"success":("base":#8bc34a),"warning":("base":#ffc107),"danger":("base":#f56c6c),"error":("base":#ff5722),"info":("base":#909399)));// @use 'element-plus/theme-chalk/dark/css-vars.css'

2.多主题配置
src/style/theme_index.scss

// 基础颜色变量(保留原始配置)$color-map:(primary:#409eff,success:#67c23a,warning:#e6a23c,danger:#f56c6c,info:#909399,text-primary:#303133,text-regular:#606266,text-secondary:#909399,text-placeholder:#c0c4cc,border-base:#dcdfe6,border-light:#e4e7ed,border-lighter:#ebeef5,border-extra-light:#f2f6fc,background-base:#f5f7fa);// 基础2$color-map2:(button-hover-bg-color:red);// 自定义变量模板(新增)$custom-vars-template:(chart:(color-1:red,color-2:red));// 主题扩展(保留原始+新增自定义)$themes:(blue:(// 原始Element变量primary:#2e72b1,success:#2bc667,warning:#ffb800,danger:#ff5722,info:#9e9e9e,text-primary:#1f2d3d,text-regular:#324057,border-radius-base:4px,box-shadow-light:02px 12px0rgba(46,114,177,0.1),// 新增自定义变量custom:map-merge($custom-vars-template,(special:(shadow-opacity:blue,highlight-color:#26c6da),my:(color1:#1fff)))),green:(primary:#007d7b,success:#8bc34a,warning:#ffc107,danger:#f44336,info:#607d8b,text-primary:#263238,text-regular:#455a64,text-secondary:#78909c,text-placeholder:#b0bec5,// 绿色主题特有调整border-radius-base:6px,box-shadow-light:02px 12px0rgba(0,125,123,0.1),// 新增自定义变量custom:map-merge($custom-vars-template,(special:(highlight-color:#26c6da),my:(color1:#f1ff)))));// 生成CSS变量(保留所有功能):root{@each $theme,$configin$themes{&[data-theme="#{$theme}"]{// 1. 输出原始Element变量@each $key,$valueinmap-merge($color-map,$config){@if$key!="custom"{--el-color-#{$key}:#{$value};}}// 解决不是--el-color的颜色@each $key,$valueinmap-merge($color-map2,$config){@if$key!="custom"{--el-#{$key}:#{$value};}}// 2. 保留衍生颜色计算// --el-color-primary-light-3: mix(#f1ff, map-get($config, primary), 30%);// --el-color-primary-light-5: mix(#f1ff, map-get($config, primary), 50%);// --el-color-primary-light-7: mix(#f1ff, map-get($config, primary), 70%);// --el-color-primary-light-9: mix(#f1ff, map-get($config, primary), 90%);// 3. 保留组件变量--el-border-radius-base:#{map-get($config,border-radius-base)};--el-box-shadow-light:#{map-get($config,box-shadow-light)};// 4. 新增自定义变量输出@each $category,$varsinmap-get($config,"custom"){@each $name,$valuein$vars{@if$value!=null{--app-#{$category}-#{$name}:#{$value};}}}}}}

3.构建配置
vite.config.ts 配置themes和additionalData

{defineConfig}from'vite'importvuefrom'@vitejs/plugin-vue'exportdefaultdefineConfig({plugins:[vue(),Components({dts:false,// 关闭自动生成 components.d.tsdirs:["src/components"],resolvers:[ElementPlusResolver({importStyle:"sass",themes:['light','dark']// 关键配置})]}),],css:{preprocessorOptions:{scss:{additionalData:`@use "@/style/element.scss" as *; @use "@/style/theme_index.scss" as *;`}}}})

4.‌主样式入口文件‌
通常在src/styles/main.scss中引入主题配置:

@use"element-plus/theme-chalk/src/index.scss";@use"./theme_index.scss"astheme;

5.‌切换控制器
/src/utils/theme.js

exportfunctionswitchTheme(themeName){// console.log('%c【' + 'themeName' + '】打印', 'color:#fff;background:#0f0', themeName)// const styleEl = document.createElement('style')// styleEl.id = 'theme-style'// styleEl.textContent = `:root { @include theme-vars(${themeName}); }`// console.log(styleEl.textContent);// const oldStyle = document.getElementById('theme-style')// oldStyle ? document.head.replaceChild(styleEl, oldStyle)// : document.head.appendChild(styleEl)// console.log('%c【' + 'oldStyle' + '】打印', 'color:#fff;background:#0f0', oldStyle)document.documentElement.setAttribute('data-theme',themeName)}

6.‌主题切换组件调用‌
在组件中使用切换逻辑:

<el-button @click="toggleTheme0">{{currentTheme==="light"?"暗色模式":"亮色模式"}}</el-button>import{switchTheme}from'../utils/theme'letthemValue=ref('blue')// 函数consttoggleTheme0=()=>{switchTheme(themValue.value==='blue'?'green':'blue')// 调用主题切换方法themValue.value=themValue.value==='blue'?'green':'blue'}

7.main.js初始化主题

document.documentElement.setAttribute('data-theme','blue')
http://www.jsqmd.com/news/859028/

相关文章:

  • 如何用嘎嘎降AI处理心理学论文:心理学研究生毕业论文降AI4.8元完整操作教程
  • 2026连云港黄金回收行业综合实力排名TOP10,2026年5月权威测评榜单 - 天天生活分享日志
  • 2026年上海代理记账公司实测推荐,公司注销、公司注册代办、代账报税、税务报告、整理乱账优质财税机构优选指南 - 品牌优企推荐
  • 分期乐购物额度如何盘活?2026安全正规渠道选择攻略 - 可可收公众号
  • 2026年佛山定制家居五金供应链深度评测:从源头工厂到渠道保护的完整指南 - 企业名录优选推荐
  • 湖南花垣县多部门联合集中销毁 400 余卷非法烟花爆竹
  • OpenClaw 本地部署避坑指南|环境配置 + 故障排查全流程
  • 初创团队如何借助taotoken的tokenplan实现可控的ai应用开发成本
  • YOLOv8无人机红外识别检测系统(项目源码+YOLO数据集+模型权重+UI界面+python+深度学习+环境配置)
  • HC2162晨芯阳内置100V功率MOS升压型LED恒流驱动器
  • 知名无线电磁流量计厂家有哪些?国内主流生产企业汇总 - 陈工日常
  • 2026 年内衣洗衣机五大品牌排名及解析 - 十大品牌榜
  • MGR原理解析
  • 2026家用电梯厂家推荐 济南春启升降机械 液压升降货梯 登车桥 - 资讯速览
  • 5分钟学会LDDC:让每一首歌都有完美歌词的终极指南
  • 海康工业相机C#开发实战:SDK集成与图像处理深度解析
  • 如何快速掌握ncmdump:网易云音乐NCM格式解密完整指南
  • HC2161晨芯阳升压型LED恒流驱动IC
  • 2036年的病榻前,陪伴我的是“贾维斯”还是我的两个儿子?
  • Windows 11终极优化指南:使用Win11Debloat免费提升电脑性能60%
  • 2026萧邦官方售后中心布局优化|全国认证网点电话(5 月更新) - 资讯速览
  • 【多所高校期刊等单位联合主办】第十届教育、管理与社会科学国际学术会议 (ISEMSS 2026) - 爱搞科研的小刘
  • Phobos:在Blender中创建专业机器人模型的完整解决方案
  • 【Lovable开发者私藏资源包】:含官方未公开API文档、调试插件源码与CI/CD配置清单
  • LangGraph 与 AutoGen 深度对比:多智能体框架选择的技术决策树
  • 为什么 HDFS 文件一旦写入就不能修改,只能追加或删除(HDFS 设计哲学:一次写入,多次读取)
  • Hertz.dev实时音频对话实战:构建智能语音助手的最佳实践指南
  • 智能散热终极指南:从噪音困扰到性能巅峰的完整配置方案
  • 如何快速搭建ChatTTS语音合成服务:完整部署指南
  • ETS2LA自动驾驶插件:为《欧洲卡车模拟2》带来智能车道保持与模块化AI驾驶体验