我使用vuex-persistedstate。但是吸气剂不起作用。 我在Vue.js Devtools检查状态,状态存在。但是吸气者没有数据。像这样。
那么如何使用getter?
vuex中的index.js
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
login,
segments,
itemList,
orderList
},
plugins: [
createPersistedState()
]
})
vue-router中的index.js(我想使用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 }
})
}
}
login.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
}
答案 0 :(得分:3)
我认为您正在访问外部state
对象。请尝试将state
添加为函数参数:
const getters = {
loginStatus: (state) => state.loginStatus,
email: (state) => state.email,
isStaff: (state) => state.isStaff,
company: (state) => state.company,
token: (state) => state.token
}