Vu路由死循环,以及addRouter怎么使用
asyncRouterMap,constantRouterMap
asyncRouterMap 是需要权限处理的路由,constantRouterMap是登陆和404页面路由数组
上代码
console.log(asyncRouterMap,'asyncRouterMap')
console.log(router);
const whiteList = ['/login'];
const userSet = store.state.user;
console.log(userSet,'userSet');
store.commit(Keys.InitialiseUerInfo,(isLogin)=>{
})
router.beforeEach(async(to, from, next) => {
console.log(to.path,to.name)
//判断有没有登陆
if (userSet.isLogin) {
console.log(userSet.isLogin,'userSet.isLogin')
if (to.path === '/login') {
next({ path: '/' })
} else {
if (userSet.userRole) {
try {
let addRoutes
let roles = userSet.userRole;
if(roles == 'ROLE_ADMIN'){
addRoutes= [...asyncRouterMap,...constantRouterMap]
}else{
addRoutes= [...constantRouterMap]
}
const accessRoutes = await store.dispatch('GenerateRoutes', roles)
// dynamically add accessible routes
router.addRoutes(accessRoutes)
router.options.routes = router.options.routes.concat(addRoutes)
console.log(addRoutes,accessRoutes)
next({ ...to, replace: true })
} catch (error) {
// remove token and go to login page to re-login
await store.dispatch(this.$storeKeys.Asyn_handleLogout).then()
next(`/login?redirect=${to.path}`)
}
}else{
next()
}
}
} else {
/* has no token*/
if (whiteList.indexOf(to.path) !== -1) {
next()
} else {
next(`/login?redirect=${to.path}`)
}
}
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论