如题,用addRoutes控制路由权限,如果正常操作登录点菜单路由拦截没有问题,add很ok,问题在于一刷新页面router就还原成初始数据了,我尝试在刷新后再次addRoutes,然而并没有效果,还是一片空白,虽然不明白为什么,讲道理是在router.beforeEach里加的,不明白为什么没效果,部分代码如下
appRouter.beforeEach((to, from, next) => {
if (to.matched.length === 0) {
next({ path: 'notFound' })
} else {
if (to.path === '/login') {
if (getToken()) {
next({ path: '/' })
} else {
next()
}
} else {
if (getToken()) {
if (appStore.getters.role.length === 0) {
commonApi.getUserInfo().then(({data: {resultCode, result, msg}}) => {
appStore.dispatch('setRoleRouter', result.role).then((res) => {
appRouter.addRoutes(res)
next()
})
})
} else {
next()
}
} else {
next({ path: '/login' })
}
}
}
})
求大佬解答
看看是不是重定向的问题,404页面放在异步路由里面的最后面!