mirror of
https://github.com/un-pany/v3-admin-vite.git
synced 2025-04-20 10:59:21 +08:00
perf: 移除路由守卫中的 next 参数
This commit is contained in:
parent
2685efa759
commit
ab306c4e2c
@ -13,21 +13,21 @@ const { setTitle } = useTitle()
|
||||
|
||||
export function registerNavigationGuard(router: Router) {
|
||||
// 全局前置守卫
|
||||
router.beforeEach(async (to, _from, next) => {
|
||||
router.beforeEach(async (to, _from) => {
|
||||
NProgress.start()
|
||||
const userStore = useUserStore()
|
||||
const permissionStore = usePermissionStore()
|
||||
// 如果没有登陆
|
||||
if (!getToken()) {
|
||||
// 如果在免登录的白名单中,则直接进入
|
||||
if (isWhiteList(to)) return next()
|
||||
if (isWhiteList(to)) return true
|
||||
// 其他没有访问权限的页面将被重定向到登录页面
|
||||
return next("/login")
|
||||
return "/login"
|
||||
}
|
||||
// 如果已经登录,并准备进入 Login 页面,则重定向到主页
|
||||
if (to.path === "/login") return next({ path: "/" })
|
||||
if (to.path === "/login") return "/"
|
||||
// 如果用户已经获得其权限角色
|
||||
if (userStore.roles.length !== 0) return next()
|
||||
if (userStore.roles.length !== 0) return true
|
||||
// 否则要重新获取权限角色
|
||||
try {
|
||||
await userStore.getInfo()
|
||||
@ -38,12 +38,12 @@ export function registerNavigationGuard(router: Router) {
|
||||
// 将 "有访问权限的动态路由" 添加到 Router 中
|
||||
permissionStore.addRoutes.forEach(route => router.addRoute(route))
|
||||
// 设置 replace: true, 因此导航将不会留下历史记录
|
||||
next({ ...to, replace: true })
|
||||
return { ...to, replace: true }
|
||||
} catch (error) {
|
||||
// 过程中发生任何错误,都直接重置 Token,并重定向到登录页面
|
||||
userStore.resetToken()
|
||||
ElMessage.error((error as Error).message || "路由守卫发生错误")
|
||||
next("/login")
|
||||
return "/login"
|
||||
}
|
||||
})
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user