From bf1f5b9f3f1d88adbe9696b225509902d43454e4 Mon Sep 17 00:00:00 2001 From: xiaoxian521 <1923740402@qq.com> Date: Fri, 19 Sep 2025 14:34:08 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=BD=93=E6=8D=95?= =?UTF-8?q?=E8=8E=B7=E6=89=80=E6=9C=89=E6=9C=AA=E5=8C=B9=E9=85=8D=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=B9=B6=E8=B7=B3=E8=BD=AC=E5=85=A8=E5=B1=8F`404`?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E7=9A=84=E6=97=B6=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/modules/remaining.ts | 29 ++++++----------------------- src/router/utils.ts | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/router/modules/remaining.ts b/src/router/modules/remaining.ts index d7c8352c3..52c8a376c 100644 --- a/src/router/modules/remaining.ts +++ b/src/router/modules/remaining.ts @@ -8,8 +8,7 @@ export default [ component: () => import("@/views/login/index.vue"), meta: { title: $t("menus.pureLogin"), - showLink: false, - rank: 101 + showLink: false } }, // 全屏403(无权访问)页面 @@ -19,19 +18,7 @@ export default [ component: () => import("@/views/error/403.vue"), meta: { title: $t("menus.pureAccessDenied"), - showLink: false, - rank: 102 - } - }, - // 全屏404(页面不存在)页面 - { - path: "/:pathMatch(.*)*", - name: "PageNotFound", - component: () => import("@/views/error/404.vue"), - meta: { - title: $t("menus.purePageNotFound"), - showLink: false, - rank: 103 + showLink: false } }, // 全屏500(服务器出错)页面 @@ -41,8 +28,7 @@ export default [ component: () => import("@/views/error/500.vue"), meta: { title: $t("menus.pureServerError"), - showLink: false, - rank: 104 + showLink: false } }, { @@ -50,8 +36,7 @@ export default [ component: Layout, meta: { title: $t("status.pureLoad"), - showLink: false, - rank: 105 + showLink: false }, children: [ { @@ -67,8 +52,7 @@ export default [ component: () => import("@/views/account-settings/index.vue"), meta: { title: $t("buttons.pureAccountSettings"), - showLink: false, - rank: 106 + showLink: false } }, // 下面是一个无layout菜单的例子(一个全屏空白页面),因为这种情况极少发生,所以只需要在前端配置即可(配置路径:src/router/modules/remaining.ts) @@ -78,8 +62,7 @@ export default [ component: () => import("@/views/empty/index.vue"), meta: { title: $t("menus.pureEmpty"), - showLink: false, - rank: 107 + showLink: false } } ] satisfies Array; diff --git a/src/router/utils.ts b/src/router/utils.ts index 375351957..43d1888de 100644 --- a/src/router/utils.ts +++ b/src/router/utils.ts @@ -139,6 +139,21 @@ function findRouteByPath(path: string, routes: RouteRecordRaw[]) { } } +/** 动态路由注册完成后,再添加全屏404(页面不存在)页面,避免刷新动态路由页面时误跳转到404页面 */ +function addPathMatch() { + if (!router.hasRoute("pathMatch")) { + router.addRoute({ + path: "/:pathMatch(.*)*", + name: "PageNotFound", + component: () => import("@/views/error/404.vue"), + meta: { + title: "menus.purePageNotFound", + showLink: false + } + }); + } +} + /** 处理动态路由(后端返回的路由) */ function handleAsyncRoutes(routeList) { if (routeList.length === 0) { @@ -178,6 +193,7 @@ function handleAsyncRoutes(routeList) { ) ]); } + addPathMatch(); } /** 初始化路由(`new Promise` 写法防止在异步请求中造成无限循环)*/ @@ -386,6 +402,7 @@ export { filterTree, initRouter, getTopMenu, + addPathMatch, isOneOfArray, getHistoryMode, addAsyncRoutes,