刷新頁面后vuex從sessionStorage獲取,為什么router.beforeEach中state的為空?vuex:const state = { state: {
token: sessionStorage.getItem('token') || '',
},
}router:router.beforeEach(function(to, from, next) {
console.log(to.meta.auth,store.state.token,to.path);
if (to.meta.auth && !store.state.token && to.path !== '/login') {
return next({
path: '/login',
query: {redirect: to.fullPath}
})
}
next();
});在用戶登陸后刷新頁面,為什么beforeEach里store.state.token為空?因為我把store寫成了state...QAQ每天都在問傻逼問題
1 回答

慕斯709654
TA貢獻1840條經驗 獲得超5個贊
const store = { // 這里是 store ?
state: {
token: sessionStorage.getItem('token') || '',
},
}
初始化 store 實例時,如果沒登錄 token 應該是空的,之后如果登錄,sessionStorage 保存到 token 后,state.token 也不會再改變了,因為沒有 mutation 去觸發重新獲取
添加回答
舉報
0/150
提交
取消