【发布时间】:2021-03-11 08:59:09
【问题描述】:
这是我的路由器保护:
router.beforeEach(async (to,from,next)=>{
await store.dispatch('GetPermission');
if(to.matched.some(record => record.meta.requireAuth)){
let permissions=store.state.permissions; //getting empty
console.log(permissions);
if(permissions.filter(per => (per.name === 'read_list').length!=0)){
next({
path:'/dashboard/create'
})
}
else{
next()
}
}
// else if(to.matched.some(record => record.meta.requireAuth)){
// if(store.token!=null){
// next({
// path:'/dashboard'
// })
// }
// else{
// next()
// }
// }
else{
next()
}
});
问题在这里,虽然我在调度方法中使用 await,但我没有获得最初为空的权限状态值 这里是 vuex 商店代码:
GetPermission(context){
axios.defaults.headers.common['Authorization']='Bearer ' + context.state.token
axios.get('http://127.0.0.1:8000/api/user').then((response)=>{
console.log(response)
context.commit('Permissions',response.data.permission)
})
//mutation:
Permissions(state,payload){
state.permissions=payload
}
//state
state:{
error:'',
token:localStorage.getItem('token') || null,
permissions:'',
success:'',
isLoggedin:'',
LoggedUser:{}
}
请帮我解决??
【问题讨论】:
-
我可以通过这种方式在 vuex 操作中使用 return 来解决这个问题
GetPermission(context){ axios.defaults.headers.common['Authorization']='Bearer ' + context.state.token return axios.get('http://127.0.0.1:8000/api/user').then((response)=>{ console.log(response) context.commit('Permissions',response.data.permission) }) }但是现在我遇到了很多请求错误,那里有什么问题?