Vue.js 异步/等待在路由器中未按预期工作。是否在vue中的每个防护之前?
这是我的路由器保护:Vue.js 异步/等待在路由器中未按预期工作。是否在vue中的每个防护之前?,vue.js,vuex,router,Vue.js,Vuex,Router,这是我的路由器保护: 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(permission
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()
}
});
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:{}
}
问题在于,尽管我在分派方法中使用了wait,但我并没有得到权限的状态值,该值最初为空
以下是vuex商店代码:
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()
}
});
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中的操作是异步的。让调用函数(操作的发起方)知道操作已完成的唯一方法是返回一个承诺并稍后解决它 下面是一个示例:myAction返回一个承诺,进行http调用,然后解析或拒绝该承诺—所有这些都是异步的
actions: {
myAction(context, data) {
return new Promise((resolve, reject) => {
// Do something here... lets say, a http call using vue-resource
this.$http("/api/something").then(response => {
// http success, call the mutator and change something in state
resolve(response); // Let the calling function know that http is done. You may send some data back
}, error => {
// http failed, let the calling function know that action did not work out
reject(error);
})
})
}
}
现在,当您的Vue组件启动myAction时,它将获得这个Promise对象,并可以知道它是否成功。以下是Vue组件的一些示例代码:
export default {
mounted: function() {
// This component just got created. Lets fetch some data here using an action
this.$store.dispatch("myAction").then(response => {
console.log("Got some data, now lets show something in this component")
}, error => {
console.error("Got nothing from server. Prompt user to check internet connection and try again")
})
}
}
另外,当没有权限匹配时,您正在调用相同的路由,在这种情况下,它总是调用相同的路由并进行无限循环。
如果权限被拒绝,则重定向到访问被拒绝页面。我可以通过以下方式在vuex操作中使用return来解决此问题
GetPermission(context){axios.defaults.headers.common['Authorization']='Bearer'+context.state.token return axios.get('http://127.0.0.1:8000/api/user)。然后((回应)=>{console.log(response)context.commit('Permissions',response.data.permission)}
但是现在我遇到了很多请求错误,有什么问题吗?