我将cookie“ darkMode”加载到中间件中的Vuex
存储index.js
import * as mut from './mutation-map'
export const state = () => ({
darkMode: false
})
export const getters = () => ({
darkMode: state => state.darkMode
})
export const mutations = {
[mut.SET__DARK_MODE]: (state, mode) => {
console.log('mutated', mode)
state.darkMode = mode
}
}
export const actions = {
toggleDarkMode({ commit, state }, mode) {
commit(mut.SET__DARK_MODE, mode)
}
}
中间件cookie.js
export default function(ctx) {
ctx.store.dispatch('toggleDarkMode', ctx.app.$cookies.get('darkMode'))
}
这在服务器上工作正常,但是在客户端上,Vuex状态仍然是默认状态!
Vue中间件仅在服务器上工作吗?我以前已经以这种方式从Vuex的Cookie中加载了一个会话,但这不适用于我的最新更新