diff --git a/yshop-drink-vue3/src/config/axios/service.ts b/yshop-drink-vue3/src/config/axios/service.ts index b3292da..32ba9a4 100644 --- a/yshop-drink-vue3/src/config/axios/service.ts +++ b/yshop-drink-vue3/src/config/axios/service.ts @@ -200,7 +200,7 @@ const handleAuthorized = () => { confirmButtonText: t('login.relogin'), type: 'warning' }).then(() => { - resetRouter() // 重置静态路由表 + resetRouter() // 重置动态路由表 isRelogin.show = false // 重新加载后由路由守卫向服务端确认会话 window.location.href = window.location.href diff --git a/yshop-drink-vue3/src/layout/components/UserInfo/src/components/LockPage.vue b/yshop-drink-vue3/src/layout/components/UserInfo/src/components/LockPage.vue index d1cdf47..ecd6859 100644 --- a/yshop-drink-vue3/src/layout/components/UserInfo/src/components/LockPage.vue +++ b/yshop-drink-vue3/src/layout/components/UserInfo/src/components/LockPage.vue @@ -50,7 +50,7 @@ async function goLogin() { await userStore.loginOut().catch(() => {}) // 登出后清理 tagsViewStore.delAllViews() - resetRouter() // 重置静态路由表 + resetRouter() // 重置动态路由表 lockStore.resetLockInfo() replace('/login') } diff --git a/yshop-drink-vue3/src/permission.ts b/yshop-drink-vue3/src/permission.ts index 48748ed..37ce0d4 100644 --- a/yshop-drink-vue3/src/permission.ts +++ b/yshop-drink-vue3/src/permission.ts @@ -1,4 +1,4 @@ -import router, { resetRouter } from './router' +import router, { addDynamicRoute, resetRouter } from './router' import type { RouteRecordRaw } from 'vue-router' import { isRelogin } from '@/config/axios/service' import { useTitle } from '@/hooks/web/useTitle' @@ -55,7 +55,7 @@ router.beforeEach(async (to, _from, next) => { const permissionStore = usePermissionStoreWithOut() permissionStore.generateRoutes(userStore.getMenus) permissionStore.getAddRouters.forEach((route) => { - router.addRoute(route as unknown as RouteRecordRaw) + addDynamicRoute(route as unknown as RouteRecordRaw) }) next({ path: to.path, query: to.query, hash: to.hash, replace: true }) diff --git a/yshop-drink-vue3/src/router/index.ts b/yshop-drink-vue3/src/router/index.ts index 8f66ca3..5ad2d11 100644 --- a/yshop-drink-vue3/src/router/index.ts +++ b/yshop-drink-vue3/src/router/index.ts @@ -11,14 +11,14 @@ const router = createRouter({ scrollBehavior: () => ({ left: 0, top: 0 }) }) +const removeDynamicRoutes: Array<() => void> = [] + +export const addDynamicRoute = (route: RouteRecordRaw): void => { + removeDynamicRoutes.push(router.addRoute(route)) +} + export const resetRouter = (): void => { - const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root'] - router.getRoutes().forEach((route) => { - const { name } = route - if (name && !resetWhiteNameList.includes(name as string)) { - router.hasRoute(name) && router.removeRoute(name) - } - }) + removeDynamicRoutes.splice(0).forEach((removeRoute) => removeRoute()) } export const setupRouter = (app: App) => { diff --git a/yshop-drink-vue3/src/store/modules/permission.ts b/yshop-drink-vue3/src/store/modules/permission.ts index 1f70965..5d6e1fb 100644 --- a/yshop-drink-vue3/src/store/modules/permission.ts +++ b/yshop-drink-vue3/src/store/modules/permission.ts @@ -35,7 +35,7 @@ export const usePermissionStore = defineStore('permission', { this.addRouters = routerMap.concat([ { path: '/:path(.*)*', - redirect: '/404', + component: () => import('@/views/Error/404.vue'), name: '404Page', meta: { hidden: true,