fix(router): 优化路由守卫避免退出登录时的无效路由请求
- 在路由守卫中添加登录状态检查,避免未登录状态下初始化路由 - 防止退出登录过程中触发路由初始化导致的token无效错误 - 只有在已登录状态下才调用initAuthRoute,提高性能 - 修复退出登录时路由守卫与清理时序的竞态条件问题
This commit is contained in:
parent
d6f3cfe25c
commit
8166c04919
@ -43,14 +43,26 @@ export function setupRouterGuard(router: Router) {
|
||||
|
||||
// 判断路由有无进行初始化
|
||||
if (!routeStore.isInitAuthRoute) {
|
||||
try {
|
||||
await routeStore.initAuthRoute()
|
||||
// 只有在已登录状态下才初始化路由,避免退出登录时的无效请求
|
||||
if (!isLogin) {
|
||||
// 未登录状态下,如果访问的不是登录页,重定向到登录页
|
||||
if (to.name !== 'login') {
|
||||
const redirect = to.name === '404' ? undefined : to.fullPath
|
||||
next({ path: '/login', query: { redirect } })
|
||||
return
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.error('路由初始化失败:', error)
|
||||
// 路由初始化失败,重定向到登录页
|
||||
next({ path: '/login' })
|
||||
return
|
||||
else {
|
||||
// 已登录状态下才初始化路由
|
||||
try {
|
||||
await routeStore.initAuthRoute()
|
||||
}
|
||||
catch (error) {
|
||||
console.error('路由初始化失败:', error)
|
||||
// 路由初始化失败,重定向到登录页
|
||||
next({ path: '/login' })
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 动态路由加载完回到根路由
|
||||
|
||||
Loading…
Reference in New Issue
Block a user