// 记录路由
let hasRoles = true
// 白名单(不需要登录就可以访问的名单)
const whiteList = ['/Login'];
router.beforeEach(async (to, from, next) => {
start();
//获取token
const token = useUserStore().token;
//判断是否登录
if (token) {
//如果等录还去登录页,直接返回首页
if (to.path === '/Login') {
next({ path: '/' })
} else {
//调用获取信息的action,重新获取动态路由
await usePermissionStore().SetDynamicRoute()
//拿到动态添加的路由
let routes = usePermissionStore().menuList
if (hasRoles) {
//重新添加动态路由
setRouterPackag(routes);
hasRoles = false
next({ ...to, replace: true })
} else {
next()
}
}
} else {
//如果没有登录,如果要去登录页,直接放行
if (whiteList.includes(to.path)) {
next()
} else {
next(`/Login`)
}
}
})
03-07
1578
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交