vuex-persistedstate :如何将getters与vuex-persistedstate一起使用?



I use vuex-persistedstate.但是getters不起作用。 我在 Vue.js Devtools 中检查了状态,状态存在。但是getters没有数据。喜欢这个。

Vue.js Chrome 中的 Devtools

那么我如何使用吸气剂呢?

索引.js 在 Vuex 中

import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
login,
segments,
itemList,
orderList
},
plugins: [
createPersistedState()
]
})

索引.js 在 vue-router 中(我想使用 getter 进行登录状态检查(

import store from '@/store'
Vue.use(Router)
const ifAuthenticated = (to, from, next) => {
if (store.getters.loginStatus) {
next()
} else {
next({
path: '/login',
query: { redirect: to.fullPath }
})
}
}

登录.js(在 Vuex 中存储模块(

const state = {
loginStatus: false,
email: '',
isStaff: false,
company: '',
token: ''
}
const getters = {
loginStatus: () => state.loginStatus,
email: () => state.email,
isStaff: () => state.isStaff,
company: () => state.company,
token: () => state.token
}

我认为您正在访问state对象之外。请尝试添加state作为函数参数:

const getters = {
loginStatus: (state) => state.loginStatus,
email: (state) => state.email,
isStaff: (state) => state.isStaff,
company: (state) => state.company,
token: (state) => state.token
}

最新更新