1.elementui btn 等 主色调颜色,去 https://element.eleme.cn/#/zh-CN/theme 官网自行操作。
正常 目录结构可以如下
2.用户自定义颜色/背景色替换。如果是 vue-cli2 则需要 在vue.config.js 配置
css: {
loaderOptions: {
sass: {
// 旧版的 scss-loader 是 data 新版的是 prependData
prependData: `@import "@/styles/setting/var.scss";`,
},
},
},
这是 需要区分主题的 颜色 scss文件 var.scss。
$primary: #B78349;
$primaryDark: green;
/* ******************************************************************************************************* */
@mixin primaryTxtColor() {
[data-theme="light"] & {
color: $primary;
}
[data-theme="dark"] & {
color: $primaryDark;
}
}
@mixin primaryBgColor() {
[data-theme="light"] & {
background-color: $primary;
}
[data-theme="dark"] & {
background-color: $primaryDark;
}
}
使用通过scss的 @include 写法 替换 之前写法:
.org-item {
height: 25px;
@include primaryBgColor();
// background-color: $primary;
}
vue 组件
<template>
<el-dropdown @command="changeTheme">
<span class="el-dropdown-link">
切换主题<i class="el-icon-arrow-down el-icon--right"></i>
</span>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item class="clearfix" command="default"
>light</el-dropdown-item
>
<el-dropdown-item class="clearfix" command="science-blue"
>dark</el-dropdown-item
>
</el-dropdown-menu>
</el-dropdown>
</template>
<script>
import { toggleClass } from "@/utils";
import "@/assets/custom-theme/index.scss";
export default {
data() {
return {
theme: "light"
};
},
mounted() {
this.theme = localStorage.getItem("lsj-theme")
? JSON.parse(localStorage.getItem("lsj-theme"))
: "light";
if (this.theme === "light") {
this.changeLight();
} else {
this.changeDark();
}
this.setAttribute(this.theme);
},
methods: {
setAttribute(theme) {
window.document.documentElement.setAttribute("data-theme", theme);
},
changeLight() {
localStorage.setItem("lsj-theme", JSON.stringify("light"));
this.setAttribute("light");
toggleClass(document.body, " ");
},
changeDark() {
localStorage.setItem("lsj-theme", JSON.stringify("dark"));
this.setAttribute("dark");
toggleClass(document.body, "custom-theme");
},
// change theme - 换肤
changeTheme(command) {
console.log(command, "command");
if (command === "default") {
this.changeLight();
return;
}
if (command === "science-blue") {
this.changeDark();
return;
}
}
}
};
</script>
/**
* @param {HTMLElement} element
* @param {string} className
*/
export function toggleClass(element, className) {
if (!element || !className) {
return;
}
element.className = className;
}