Skip to content

Commit

Permalink
fix(Router): use different route name of not found page to avoid warning
Browse files Browse the repository at this point in the history
  • Loading branch information
suyuan32 committed Jun 13, 2024
1 parent 2c348aa commit 5381f3d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/router/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
7 changes: 4 additions & 3 deletions src/router/guard/permissionGuard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions src/router/routes/basic.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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',
Expand Down

0 comments on commit 5381f3d

Please sign in to comment.