From 5381f3d13bf8334af9ad976072c980e67c50a75d Mon Sep 17 00:00:00 2001 From: Ryan Su Date: Thu, 13 Jun 2024 11:48:58 +0800 Subject: [PATCH] fix(Router): use different route name of not found page to avoid warning --- src/router/constant.ts | 1 + src/router/guard/permissionGuard.ts | 7 ++++--- src/router/routes/basic.ts | 12 +++++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/router/constant.ts b/src/router/constant.ts index e57bc2601..720d475ee 100644 --- a/src/router/constant.ts +++ b/src/router/constant.ts @@ -3,6 +3,7 @@ export const REDIRECT_NAME = 'Redirect'; export const PARENT_LAYOUT_NAME = 'ParentLayout'; export const PAGE_NOT_FOUND_NAME = 'PageNotFound'; +export const PAGE_NOT_FOUND_NAME_CHILDREN = 'PageNotFoundChildren'; export const EXCEPTION_COMPONENT = () => import('@/views/sys/exception/Exception.vue'); diff --git a/src/router/guard/permissionGuard.ts b/src/router/guard/permissionGuard.ts index 80be2ec63..6f322aa69 100644 --- a/src/router/guard/permissionGuard.ts +++ b/src/router/guard/permissionGuard.ts @@ -8,6 +8,7 @@ import { useUserStoreWithOut } from '@/store/modules/user'; import { PAGE_NOT_FOUND_ROUTE } from '@/router/routes/basic'; import { RootRoute } from '@/router/routes'; +import { PAGE_NOT_FOUND_NAME_CHILDREN } from '../constant'; const LOGIN_PATH = PageEnum.BASE_LOGIN; @@ -62,10 +63,10 @@ export function createPermissionGuard(router: Router) { path: LOGIN_PATH, replace: true, }; - if (to.path) { + if (to.fullPath) { redirectData.query = { ...redirectData.query, - redirect: to.path, + redirect: to.fullPath, }; } next(redirectData); @@ -107,7 +108,7 @@ export function createPermissionGuard(router: Router) { permissionStore.setDynamicAddedRoute(true); - if (to.name === PAGE_NOT_FOUND_ROUTE.name) { + if (to.name === PAGE_NOT_FOUND_ROUTE.name || to.name === PAGE_NOT_FOUND_NAME_CHILDREN) { // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容 next({ path: to.fullPath, replace: true, query: to.query }); } else { diff --git a/src/router/routes/basic.ts b/src/router/routes/basic.ts index b8066c12f..e53089921 100644 --- a/src/router/routes/basic.ts +++ b/src/router/routes/basic.ts @@ -1,11 +1,17 @@ import type { AppRouteRecordRaw } from '@/router/types'; -import { REDIRECT_NAME, LAYOUT, EXCEPTION_COMPONENT, PAGE_NOT_FOUND_NAME } from '@/router/constant'; +import { + REDIRECT_NAME, + LAYOUT, + EXCEPTION_COMPONENT, + PAGE_NOT_FOUND_NAME, + PAGE_NOT_FOUND_NAME_CHILDREN, +} from '@/router/constant'; // 404 on a page export const PAGE_NOT_FOUND_ROUTE: AppRouteRecordRaw = { path: '/:path(.*)*', - name: PAGE_NOT_FOUND_NAME, component: LAYOUT, + name: PAGE_NOT_FOUND_NAME, meta: { title: 'ErrorPage', hideBreadcrumb: true, @@ -14,7 +20,7 @@ export const PAGE_NOT_FOUND_ROUTE: AppRouteRecordRaw = { children: [ { path: '/:path(.*)*', - name: PAGE_NOT_FOUND_NAME, + name: PAGE_NOT_FOUND_NAME_CHILDREN, component: EXCEPTION_COMPONENT, meta: { title: 'ErrorPage',