|
@@ -38,22 +38,30 @@ new Vue({
|
|
|
},
|
|
|
watch: {
|
|
|
"$store.state.themes.theme": {
|
|
|
- handler(newTheme) {
|
|
|
- console.log(newTheme, "newTheme");
|
|
|
- this.applyTheme(newTheme);
|
|
|
+ handler(newTheme, oldTheme) {
|
|
|
+ this.applyTheme(newTheme, oldTheme);
|
|
|
},
|
|
|
immediate: true,
|
|
|
},
|
|
|
},
|
|
|
methods: {
|
|
|
- async applyTheme(theme) {
|
|
|
+ async applyTheme(theme, oldTheme) {
|
|
|
const themeFile = theme || this.$store.state.themes.theme;
|
|
|
- console.log(themeFile, "themeFile");
|
|
|
+ console.log(theme, oldTheme, "themeFile");
|
|
|
|
|
|
// 动态加载新的主题样式
|
|
|
try {
|
|
|
// 动态导入 SCSS 文件
|
|
|
const theme = await import(`@/themes/${themeFile}.scss`);
|
|
|
+ Vue.prototype.$primaryColor = getComputedStyle(
|
|
|
+ document.documentElement
|
|
|
+ ).getPropertyValue("--primary-color");
|
|
|
+ Vue.prototype.$backgroundColor = getComputedStyle(
|
|
|
+ document.documentElement
|
|
|
+ ).getPropertyValue("--background-color");
|
|
|
+ Vue.prototype.$textColor = getComputedStyle(
|
|
|
+ document.documentElement
|
|
|
+ ).getPropertyValue("--text-color");
|
|
|
|
|
|
// 移除之前的主题样式
|
|
|
let oldThemeStyle = document.getElementById("theme-style");
|
|
@@ -67,6 +75,10 @@ new Vue({
|
|
|
style.type = "text/css";
|
|
|
style.innerHTML = theme.default;
|
|
|
document.head.appendChild(style);
|
|
|
+ // store.commit()
|
|
|
+ if (theme !== oldTheme && oldTheme !== undefined) {
|
|
|
+ this.$router.go(0);
|
|
|
+ }
|
|
|
} catch (error) {
|
|
|
console.error("Failed to load theme", error);
|
|
|
}
|