项目中安装插件
npm i element-theme -Dnpm i element-theme-chalk -D"element-theme": "^2.0.1","element-theme-chalk": "^2.15.6",
通过 node_modules/.bin/et 访问到命令。执行 -i 初始化变量文件。默认输出到 element-variables.scss,当然你可以传参数指定文件输出目录。
node_modules/.bin/et -i
生成 element-variables.scss 文件
如果此时出现报错信息:primordials is not defined 解决方案: 使用的node版本更换为11.15.0
修改变量
直接编辑 element-variables.scss 文件,例如修改主题色为红色。
$--color-primary: #d0021b !default;
编译主题
执行主题编译命令生成主题,根目录会生成一个theme的文件夹 。
node_modules/.bin/et
引入自定义主题
把生成的主题按颜色改名放置 src/theme 目录下。
在 main.js 中import ‘所在路径/index.css’
import Element from 'element-ui';// import 'element-ui/lib/theme-chalk/index.css'; // 默认的样式import "@/theme/index.css" // 自定义主题样式
👉动态换肤器方式
添加换肤组件

index.vue
<template><div class="ThemePick"><el-color-picker class="theme-picker" popper-class="theme-picker-dropdown" v-model="theme" :size="size"></el-color-picker></div></template><script>const version = require('element-ui/package.json').version // element-ui version from node_modulesconst ORIGINAL_THEME = '#409EFF' // default colorexport default {name: 'ThemePick',props: {defaultTheme: {type: String,default: null},size: {type: String,default: 'small'}},components: {},data() {return {chalk: '', // content of theme-chalk csstheme: ORIGINAL_THEME,showSuccess: true // 是否弹出换肤成功消息}},computed: {},watch: {theme(val, oldVal) {console.log("theme = ", val, oldVal);if (typeof val !== 'string') returnconst themeCluster = this.getThemeCluster(val.replace('#', ''))const originalCluster = this.getThemeCluster(oldVal.replace('#', ''))console.log(themeCluster, originalCluster)const getHandler = (variable, id) => {return () => {const originalCluster = this.getThemeCluster(ORIGINAL_THEME.replace('#', ''))const newStyle = this.updateStyle(this[variable], originalCluster, themeCluster)let styleTag = document.getElementById(id)if (!styleTag) {styleTag = document.createElement('style')styleTag.setAttribute('id', id)document.head.appendChild(styleTag)}styleTag.innerText = newStyle}}const chalkHandler = getHandler('chalk', 'chalk-style')if (!this.chalk) {const url = `https://unpkg.com/element-ui@${version}/lib/theme-chalk/index.css`this.getCSSString(url, chalkHandler, 'chalk')} else {chalkHandler()}const styles = [].slice.call(document.querySelectorAll('style')).filter(style => {const text = style.innerTextreturn new RegExp(oldVal, 'i').test(text) && !/Chalk Variables/.test(text)})styles.forEach(style => {const { innerText } = styleif (typeof innerText !== 'string') returnstyle.innerText = this.updateStyle(innerText, originalCluster, themeCluster)})// 响应外部操作this.$emit('onThemeChange', val)if (this.showSuccess) {this.$message({message: '换肤成功',type: 'success'})} else {this.showSuccess = true}}},created() { },mounted() {if (this.defaultTheme != null) {this.theme = this.defaultThemethis.$emit('onThemeChange', this.theme)this.showSuccess = false}},methods: {updateStyle(style, oldCluster, newCluster) {let newStyle = styleoldCluster.forEach((color, index) => {newStyle = newStyle.replace(new RegExp(color, 'ig'), newCluster[index])})return newStyle},getCSSString(url, callback, variable) {const xhr = new XMLHttpRequest()xhr.onreadystatechange = () => {if (xhr.readyState === 4 && xhr.status === 200) {this[variable] = xhr.responseText.replace(/@font-face{[^}]+}/, '')callback()}}xhr.open('GET', url)xhr.send()},getThemeCluster(theme) {const tintColor = (color, tint) => {let red = parseInt(color.slice(0, 2), 16)let green = parseInt(color.slice(2, 4), 16)let blue = parseInt(color.slice(4, 6), 16)if (tint === 0) { // when primary color is in its rgb spacereturn [red, green, blue].join(',')} else {red += Math.round(tint * (255 - red))green += Math.round(tint * (255 - green))blue += Math.round(tint * (255 - blue))red = red.toString(16)green = green.toString(16)blue = blue.toString(16)return `#${red}${green}${blue}`}}const shadeColor = (color, shade) => {let red = parseInt(color.slice(0, 2), 16)let green = parseInt(color.slice(2, 4), 16)let blue = parseInt(color.slice(4, 6), 16)red = Math.round((1 - shade) * red)green = Math.round((1 - shade) * green)blue = Math.round((1 - shade) * blue)red = red.toString(16)green = green.toString(16)blue = blue.toString(16)return `#${red}${green}${blue}`}const clusters = [theme]for (let i = 0; i <= 9; i++) {clusters.push(tintColor(theme, Number((i / 10).toFixed(2))))}clusters.push(shadeColor(theme, 0.1))return clusters}},updated() { },beforeDestroy() { },}</script><style lang='less' rel='stylesheet/less' scoped>@import "./index.less";</style>
index.less
.ThemePick {.theme-picker {.el-color-picker__trigger {vertical-align: middle;}}.theme-picker-dropdown {.el-color-dropdown__link-btn {display: none;}}}
在组件中引用
<theme-pick @onThemeChange="onThemeChange"></theme-pick>
