fix: resolving Vue Router warn

移除因为动态加载路由而产生的vue-router警告
This commit is contained in:
无木 2021-07-14 20:32:58 +08:00
parent 6350224a1b
commit 237e65eac9
6 changed files with 56 additions and 25 deletions

View File

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

View File

@ -2,6 +2,8 @@ export const REDIRECT_NAME = 'Redirect';
export const PARENT_LAYOUT_NAME = 'ParentLayout'; export const PARENT_LAYOUT_NAME = 'ParentLayout';
export const PAGE_NOT_FOUND_NAME = 'PageNotFound';
export const EXCEPTION_COMPONENT = () => import('../views/sys/exception/Exception.vue'); export const EXCEPTION_COMPONENT = () => import('../views/sys/exception/Exception.vue');
/** /**

View File

@ -8,6 +8,7 @@ import { useUserStoreWithOut } from '/@/store/modules/user';
import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic'; import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
import { RootRoute } from '/@/router/routes'; import { RootRoute } from '/@/router/routes';
import { omit } from 'lodash-es';
const LOGIN_PATH = PageEnum.BASE_LOGIN; const LOGIN_PATH = PageEnum.BASE_LOGIN;
@ -18,26 +19,20 @@ const whitePathList: PageEnum[] = [LOGIN_PATH];
export function createPermissionGuard(router: Router) { export function createPermissionGuard(router: Router) {
const userStore = useUserStoreWithOut(); const userStore = useUserStoreWithOut();
const permissionStore = usePermissionStoreWithOut(); const permissionStore = usePermissionStoreWithOut();
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from) => {
// Jump to the 404 page after processing the login
if (from.path === LOGIN_PATH && to.name === PAGE_NOT_FOUND_ROUTE.name) {
next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
return;
}
if ( if (
from.path === ROOT_PATH && from.path === ROOT_PATH &&
to.path === PageEnum.BASE_HOME && to.path === PageEnum.BASE_HOME &&
userStore.getUserInfo.homePath && userStore.getUserInfo.homePath &&
userStore.getUserInfo.homePath !== PageEnum.BASE_HOME userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
) { ) {
next(userStore.getUserInfo.homePath); // next(userStore.getUserInfo.homePath);
return; return userStore.getUserInfo.homePath;
} }
// Whitelist can be directly entered // Whitelist can be directly entered
if (whitePathList.includes(to.path as PageEnum)) { if (whitePathList.includes(to.path as PageEnum)) {
next(); // next();
return; return;
} }
@ -47,7 +42,7 @@ export function createPermissionGuard(router: Router) {
if (!token) { if (!token) {
// You can access without permission. You need to set the routing meta.ignoreAuth to true // You can access without permission. You need to set the routing meta.ignoreAuth to true
if (to.meta.ignoreAuth) { if (to.meta.ignoreAuth) {
next(); // next();
return; return;
} }
@ -62,12 +57,18 @@ export function createPermissionGuard(router: Router) {
redirect: to.path, redirect: to.path,
}; };
} }
next(redirectData); //next(redirectData);
return; return redirectData;
}
// Jump to the 404 page after processing the login
if (from.path === LOGIN_PATH && to.name === PAGE_NOT_FOUND_ROUTE.name) {
//next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
return userStore.getUserInfo.homePath || PageEnum.BASE_HOME;
} }
if (permissionStore.getIsDynamicAddedRoute) { if (permissionStore.getIsDynamicAddedRoute) {
next(); // next();
return; return;
} }
@ -79,8 +80,12 @@ export function createPermissionGuard(router: Router) {
const redirectPath = (from.query.redirect || to.path) as string; const redirectPath = (from.query.redirect || to.path) as string;
const redirect = decodeURIComponent(redirectPath); const redirect = decodeURIComponent(redirectPath);
const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }; const nextData =
to.path === redirect
? { ...omit(to, ['name', 'params']), replace: true }
: { path: redirect };
permissionStore.setDynamicAddedRoute(true); permissionStore.setDynamicAddedRoute(true);
next(nextData); // next(nextData);
return nextData;
}); });
} }

View File

@ -1,11 +1,16 @@
import type { AppRouteRecordRaw } from '/@/router/types'; import type { AppRouteRecordRaw } from '/@/router/types';
import { t } from '/@/hooks/web/useI18n'; import { t } from '/@/hooks/web/useI18n';
import { REDIRECT_NAME, LAYOUT, EXCEPTION_COMPONENT } from '/@/router/constant'; import {
REDIRECT_NAME,
LAYOUT,
EXCEPTION_COMPONENT,
PAGE_NOT_FOUND_NAME,
} from '/@/router/constant';
// 404 on a page // 404 on a page
export const PAGE_NOT_FOUND_ROUTE: AppRouteRecordRaw = { export const PAGE_NOT_FOUND_ROUTE: AppRouteRecordRaw = {
path: '/:path(.*)*', path: '/:path(.*)*',
name: 'ErrorPage', name: PAGE_NOT_FOUND_NAME,
component: LAYOUT, component: LAYOUT,
meta: { meta: {
title: 'ErrorPage', title: 'ErrorPage',
@ -15,11 +20,12 @@ export const PAGE_NOT_FOUND_ROUTE: AppRouteRecordRaw = {
children: [ children: [
{ {
path: '/:path(.*)*', path: '/:path(.*)*',
name: 'ErrorPage', name: PAGE_NOT_FOUND_NAME,
component: EXCEPTION_COMPONENT, component: EXCEPTION_COMPONENT,
meta: { meta: {
title: 'ErrorPage', title: 'ErrorPage',
hideBreadcrumb: true, hideBreadcrumb: true,
hideMenu: true,
}, },
}, },
], ],

View File

@ -37,4 +37,10 @@ export const LoginRoute: AppRouteRecordRaw = {
}; };
// Basic routing without permission // Basic routing without permission
export const basicRoutes = [LoginRoute, RootRoute, ...mainOutRoutes, REDIRECT_ROUTE]; export const basicRoutes = [
LoginRoute,
RootRoute,
...mainOutRoutes,
REDIRECT_ROUTE,
PAGE_NOT_FOUND_ROUTE,
];

View File

@ -11,6 +11,8 @@ import { doLogout, getUserInfo, loginApi } from '/@/api/sys/user';
import { useI18n } from '/@/hooks/web/useI18n'; import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage'; import { useMessage } from '/@/hooks/web/useMessage';
import { router } from '/@/router'; import { router } from '/@/router';
import { usePermissionStore } from '/@/store/modules/permission';
import { RouteRecordRaw } from 'vue-router';
interface UserState { interface UserState {
userInfo: Nullable<UserInfo>; userInfo: Nullable<UserInfo>;
@ -87,10 +89,19 @@ export const useUserStore = defineStore({
const userInfo = await this.getUserInfoAction(); const userInfo = await this.getUserInfoAction();
const sessionTimeout = this.sessionTimeout; const sessionTimeout = this.sessionTimeout;
sessionTimeout && this.setSessionTimeout(false); if (sessionTimeout) {
!sessionTimeout && this.setSessionTimeout(false);
goHome && } else if (goHome) {
(await router.replace(userInfo.homePath || PageEnum.BASE_HOME)); const permissionStore = usePermissionStore();
if (!permissionStore.isDynamicAddedRoute) {
const routes = await permissionStore.buildRoutesAction();
routes.forEach((route) => {
router.addRoute(route as unknown as RouteRecordRaw);
});
permissionStore.setDynamicAddedRoute(true);
}
await router.replace(userInfo.homePath || PageEnum.BASE_HOME);
}
return userInfo; return userInfo;
} catch (error) { } catch (error) {
return Promise.reject(error); return Promise.reject(error);