Skip to content

Commit 237e65e

Browse files
committed
fix: resolving Vue Router warn
移除因为动态加载路由而产生的vue-router警告
1 parent 6350224 commit 237e65e

File tree

6 files changed

+56
-25
lines changed

6 files changed

+56
-25
lines changed

CHANGELOG.zh_CN.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
- 修复悬停触发模式下左侧混合菜单会在没有子菜单且被激活时直接跳转路由
2323
- **Breadcrumb** 修复带有重定向的菜单点击无法跳转的问题
2424
- **Markdown** 修复初始化异常以及不能正确地动态设置 value 的问题
25+
- **Modal** 确保 props 正确被传递
2526
- **其它**
2627
- 修复菜单默认折叠的配置不起作用的问题
2728
- 修复`safari`浏览器报错导致网站打不开
2829
- 修复在 window 上,拉取代码后 eslint 因 endOfLine 而保错问题
29-
- **Modal** 确保 props 正确被传递
30+
- 修复因动态路由而产生的 `Vue Router warn`
3031

3132
### 🎫 Chores
3233

src/router/constant.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export const REDIRECT_NAME = 'Redirect';
22

33
export const PARENT_LAYOUT_NAME = 'ParentLayout';
44

5+
export const PAGE_NOT_FOUND_NAME = 'PageNotFound';
6+
57
export const EXCEPTION_COMPONENT = () => import('../views/sys/exception/Exception.vue');
68

79
/**

src/router/guard/permissionGuard.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useUserStoreWithOut } from '/@/store/modules/user';
88
import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
99

1010
import { RootRoute } from '/@/router/routes';
11+
import { omit } from 'lodash-es';
1112

1213
const LOGIN_PATH = PageEnum.BASE_LOGIN;
1314

@@ -18,26 +19,20 @@ const whitePathList: PageEnum[] = [LOGIN_PATH];
1819
export function createPermissionGuard(router: Router) {
1920
const userStore = useUserStoreWithOut();
2021
const permissionStore = usePermissionStoreWithOut();
21-
router.beforeEach(async (to, from, next) => {
22-
// Jump to the 404 page after processing the login
23-
if (from.path === LOGIN_PATH && to.name === PAGE_NOT_FOUND_ROUTE.name) {
24-
next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
25-
return;
26-
}
27-
22+
router.beforeEach(async (to, from) => {
2823
if (
2924
from.path === ROOT_PATH &&
3025
to.path === PageEnum.BASE_HOME &&
3126
userStore.getUserInfo.homePath &&
3227
userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
3328
) {
34-
next(userStore.getUserInfo.homePath);
35-
return;
29+
// next(userStore.getUserInfo.homePath);
30+
return userStore.getUserInfo.homePath;
3631
}
3732

3833
// Whitelist can be directly entered
3934
if (whitePathList.includes(to.path as PageEnum)) {
40-
next();
35+
// next();
4136
return;
4237
}
4338

@@ -47,7 +42,7 @@ export function createPermissionGuard(router: Router) {
4742
if (!token) {
4843
// You can access without permission. You need to set the routing meta.ignoreAuth to true
4944
if (to.meta.ignoreAuth) {
50-
next();
45+
// next();
5146
return;
5247
}
5348

@@ -62,12 +57,18 @@ export function createPermissionGuard(router: Router) {
6257
redirect: to.path,
6358
};
6459
}
65-
next(redirectData);
66-
return;
60+
//next(redirectData);
61+
return redirectData;
62+
}
63+
64+
// Jump to the 404 page after processing the login
65+
if (from.path === LOGIN_PATH && to.name === PAGE_NOT_FOUND_ROUTE.name) {
66+
//next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
67+
return userStore.getUserInfo.homePath || PageEnum.BASE_HOME;
6768
}
6869

6970
if (permissionStore.getIsDynamicAddedRoute) {
70-
next();
71+
// next();
7172
return;
7273
}
7374

@@ -79,8 +80,12 @@ export function createPermissionGuard(router: Router) {
7980

8081
const redirectPath = (from.query.redirect || to.path) as string;
8182
const redirect = decodeURIComponent(redirectPath);
82-
const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
83+
const nextData =
84+
to.path === redirect
85+
? { ...omit(to, ['name', 'params']), replace: true }
86+
: { path: redirect };
8387
permissionStore.setDynamicAddedRoute(true);
84-
next(nextData);
88+
// next(nextData);
89+
return nextData;
8590
});
8691
}

src/router/routes/basic.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import type { AppRouteRecordRaw } from '/@/router/types';
22
import { t } from '/@/hooks/web/useI18n';
3-
import { REDIRECT_NAME, LAYOUT, EXCEPTION_COMPONENT } from '/@/router/constant';
3+
import {
4+
REDIRECT_NAME,
5+
LAYOUT,
6+
EXCEPTION_COMPONENT,
7+
PAGE_NOT_FOUND_NAME,
8+
} from '/@/router/constant';
49

510
// 404 on a page
611
export const PAGE_NOT_FOUND_ROUTE: AppRouteRecordRaw = {
712
path: '/:path(.*)*',
8-
name: 'ErrorPage',
13+
name: PAGE_NOT_FOUND_NAME,
914
component: LAYOUT,
1015
meta: {
1116
title: 'ErrorPage',
@@ -15,11 +20,12 @@ export const PAGE_NOT_FOUND_ROUTE: AppRouteRecordRaw = {
1520
children: [
1621
{
1722
path: '/:path(.*)*',
18-
name: 'ErrorPage',
23+
name: PAGE_NOT_FOUND_NAME,
1924
component: EXCEPTION_COMPONENT,
2025
meta: {
2126
title: 'ErrorPage',
2227
hideBreadcrumb: true,
28+
hideMenu: true,
2329
},
2430
},
2531
],

src/router/routes/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,10 @@ export const LoginRoute: AppRouteRecordRaw = {
3737
};
3838

3939
// Basic routing without permission
40-
export const basicRoutes = [LoginRoute, RootRoute, ...mainOutRoutes, REDIRECT_ROUTE];
40+
export const basicRoutes = [
41+
LoginRoute,
42+
RootRoute,
43+
...mainOutRoutes,
44+
REDIRECT_ROUTE,
45+
PAGE_NOT_FOUND_ROUTE,
46+
];

src/store/modules/user.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { doLogout, getUserInfo, loginApi } from '/@/api/sys/user';
1111
import { useI18n } from '/@/hooks/web/useI18n';
1212
import { useMessage } from '/@/hooks/web/useMessage';
1313
import { router } from '/@/router';
14+
import { usePermissionStore } from '/@/store/modules/permission';
15+
import { RouteRecordRaw } from 'vue-router';
1416

1517
interface UserState {
1618
userInfo: Nullable<UserInfo>;
@@ -87,10 +89,19 @@ export const useUserStore = defineStore({
8789
const userInfo = await this.getUserInfoAction();
8890

8991
const sessionTimeout = this.sessionTimeout;
90-
sessionTimeout && this.setSessionTimeout(false);
91-
!sessionTimeout &&
92-
goHome &&
93-
(await router.replace(userInfo.homePath || PageEnum.BASE_HOME));
92+
if (sessionTimeout) {
93+
this.setSessionTimeout(false);
94+
} else if (goHome) {
95+
const permissionStore = usePermissionStore();
96+
if (!permissionStore.isDynamicAddedRoute) {
97+
const routes = await permissionStore.buildRoutesAction();
98+
routes.forEach((route) => {
99+
router.addRoute(route as unknown as RouteRecordRaw);
100+
});
101+
permissionStore.setDynamicAddedRoute(true);
102+
}
103+
await router.replace(userInfo.homePath || PageEnum.BASE_HOME);
104+
}
94105
return userInfo;
95106
} catch (error) {
96107
return Promise.reject(error);

0 commit comments

Comments
 (0)