刷新頁(yè)面后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();
});在用戶登陸后刷新頁(yè)面,為什么beforeEach里store.state.token為空?因?yàn)槲野裺tore寫成了state...QAQ每天都在問(wèn)傻逼問(wèn)題
1 回答

慕斯709654
TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
const store = { // 這里是 store ?
state: {
token: sessionStorage.getItem('token') || '',
},
}
初始化 store 實(shí)例時(shí),如果沒(méi)登錄 token 應(yīng)該是空的,之后如果登錄,sessionStorage 保存到 token 后,state.token 也不會(huì)再改變了,因?yàn)闆](méi)有 mutation 去觸發(fā)重新獲取
添加回答
舉報(bào)
0/150
提交
取消