diff --git a/src/router/guard/dynamic.ts b/src/router/guard/dynamic.ts index 6d9c2c5d3..965c4914c 100644 --- a/src/router/guard/dynamic.ts +++ b/src/router/guard/dynamic.ts @@ -18,7 +18,8 @@ export async function createDynamicRouteGuard( if (!route.isInitAuthRoute) { // 未登录情况下直接回到登录页,登录成功后再加载权限路由 if (!isLogin) { - if (to.name === routeName('login')) { + const toName = to.name as AuthRoute.RouteKey; + if (route.isValidConstantRoute(toName) && !to.meta.requiresAuth) { next(); } else { const redirect = to.fullPath; diff --git a/src/store/modules/route/index.ts b/src/store/modules/route/index.ts index 85c4a460b..33c3a521e 100644 --- a/src/store/modules/route/index.ts +++ b/src/store/modules/route/index.ts @@ -53,14 +53,30 @@ export const useRouteStore = defineStore('route-store', { /** 重置路由数据,保留固定路由 */ resetRoutes() { const routes = router.getRoutes(); - const constantRouteNames = getConstantRouteNames(constantRoutes); routes.forEach(route => { const name: AuthRoute.RouteKey = (route.name || 'root') as AuthRoute.RouteKey; - if (!constantRouteNames.includes(name)) { + if (!this.isConstantRoute(name)) { router.removeRoute(name); } }); }, + /** + * 是否是固定路由 + * @param name 路由名称 + */ + isConstantRoute(name: AuthRoute.RouteKey) { + const constantRouteNames = getConstantRouteNames(constantRoutes); + return constantRouteNames.includes(name); + }, + /** + * 是否是有效的固定路由 + * @param name 路由名称 + */ + isValidConstantRoute(name: AuthRoute.RouteKey) { + const NOT_FOUND_PAGE_NAME: AuthRoute.RouteKey = 'not-found-page'; + const constantRouteNames = getConstantRouteNames(constantRoutes); + return constantRouteNames.includes(name) && name !== NOT_FOUND_PAGE_NAME; + }, /** * 处理权限路由 * @param routes - 权限路由