diff --git a/src/components/core/d2-layout-main/index.vue b/src/components/core/d2-layout-main/index.vue index 15e4ee42..6493b856 100644 --- a/src/components/core/d2-layout-main/index.vue +++ b/src/components/core/d2-layout-main/index.vue @@ -47,6 +47,7 @@ export default { } }, mounted () { + // 加载主题 this.loadTheme() }, methods: { diff --git a/src/components/core/d2-theme-list/index.vue b/src/components/core/d2-theme-list/index.vue index 5e79ec70..5abf11cd 100644 --- a/src/components/core/d2-theme-list/index.vue +++ b/src/components/core/d2-theme-list/index.vue @@ -24,9 +24,6 @@ export default { } } }, - mounted () { - this.setTheme('d2') - }, computed: { ...mapState({ themeList: state => state.theme.list, diff --git a/src/store/modules/theme.js b/src/store/modules/theme.js index f1ba0088..5eae0945 100644 --- a/src/store/modules/theme.js +++ b/src/store/modules/theme.js @@ -14,20 +14,33 @@ export default { preview: '/static/image/theme-preview/star@2x.png' } ], - name: 'star' + name: '' }, mutations: { + // 从 cookie 加载主题 loadTheme (state) { const name = Cookies.get('themeName') if (name) { - this.commit('setTheme', name) + // 设置 store + state.name = name + // 激活主题 + this.commit('activeTheme') } else { + // 设置新的主题为列表第一项 this.commit('setTheme', state.list[0].value) } }, - // 设置主题 + // 设置新的主题 setTheme (state, name) { + // 设置 store state.name = name + // 设置 Cookie + Cookies.set('themeName', name) + // 激活主题 + this.commit('activeTheme') + }, + // 激活当前主题 + activeTheme (state) { document.body.className = `theme-${state.name}` } }