【发布时间】:2019-03-18 21:39:33
【问题描述】:
我已按照文档中的定义添加了 vuex-persistedstate。 (已确认且有效)
export default new Vuex.Store({
modules: {
auth,
},
plugins: [VuexPersistedState()]
});
我已经设置了一个路由器导航保护,以便在登录时重定向到主页
/* Auth based route handler */
router.beforeEach((to, from, next) => {
if (to.meta.hasOwnProperty('requiresAuth')) {
if (to.meta.requiresAuth === true) {
let isAuthenticated = authStore.getters.isAuthenticated
if (isAuthenticated(authStore.state) === true) {
next()
} else {
next({name: 'login'})
}
} else {
let isAuthenticated = authStore.getters.isAuthenticated
console.log(authStore.state)
if (isAuthenticated(authStore.state) === true) {
next({name: 'home'})
} else {
next()
}
}
} else {
next()
}
})
vuex 持久化状态从本地存储恢复存储,但不是在导航守卫之前!
我可以提供源代码的任何必要部分进行评估。请根据需要评论您的请求。也欢迎实验性解决方案。这对我来说只是一个个人培训应用程序!
感谢您的帮助!
【问题讨论】:
-
vuex 和 vue-router 的 Vue.use() 的相对顺序是什么?您可能想尝试翻转它(第一次切割)。
-
我无法重现您的问题。事实上,看起来 vue-router 和 vuex 的相对顺序甚至都没有关系。你会尝试将商店的状态发送给 getter,而 getter 本身可以访问商店,这对我来说看起来很奇怪,所以也许那里发生了一些奇怪的事情?
-
在没有计算范围的情况下直接访问 getter 不会传递带有状态的 getter。从 store 对象访问 getter 就像从文件访问导出的函数一样。因此,以这种方式调用 getter 会将状态对象分配为未定义。因此我需要传递状态对象。
标签: vue.js vuejs2 vuex vue-router