From 01afc1f5be1fcf02a718060b43a5b7b7495d09d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BF=97=E5=BC=BA?= <357099073@qq.com> Date: Thu, 9 Apr 2026 15:04:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E5=B9=B3=E5=8F=B0=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.js | 15 ++++++++-- src/utils/pathResolver.js | 1 + src/views/Main.vue | 62 +++++++-------------------------------- src/views/login/index.vue | 24 ++++++++++++++- 4 files changed, 46 insertions(+), 56 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index a5a9037..c038c5a 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -130,7 +130,7 @@ function addDynamicRoutes(menus) { path: "/", name: "Main", component: () => import("@/views/Main.vue"), - redirect: "/dashboard", + redirect: "/home", meta: { requiresAuth: true }, children: [...staticMainChildren, ...dynamicRoutes] // 合并静态和动态子路由 }); @@ -190,8 +190,17 @@ router.beforeEach(async (to, from, next) => { } if (!dynamicRoutesAdded) { - await loadAndAddDynamicRoutes(); - // 路由加载后重新导航,确保路由匹配正确 + try { + await loadAndAddDynamicRoutes(); + } catch (error) { + console.error('动态路由加载失败:', error); + // token 无效时跳转登录 + if (error?.message === 'token无效' || error?.response?.status === 401) { + next({ path: '/login' }); + return; + } + } + // 路由加载后重新导航,确保路由匹配正确 next({ path: to.path, replace: true }); return; } diff --git a/src/utils/pathResolver.js b/src/utils/pathResolver.js index 0806aae..213bfda 100644 --- a/src/utils/pathResolver.js +++ b/src/utils/pathResolver.js @@ -2,6 +2,7 @@ * 通用的别名路径解析工具 * 用于在动态导入时解析 @ 别名路径 */ +import { h } from 'vue'; // 使用 import.meta.glob 预加载所有组件 const viewsModules = import.meta.glob('../views/**/*.vue'); diff --git a/src/views/Main.vue b/src/views/Main.vue index 91cd896..3c2471e 100644 --- a/src/views/Main.vue +++ b/src/views/Main.vue @@ -119,31 +119,11 @@ const handleAsideMenuClick = async (menuItem) => { return; } - // 检查路由是否存在,如果不存在等待一下再尝试 - let routeExists = router.resolve(targetPath).matched.length > 0; - - if (!routeExists) { - // 等待路由加载(最多等待500ms) - for (let i = 0; i < 5; i++) { - await new Promise(resolve => setTimeout(resolve, 100)); - routeExists = router.resolve(targetPath).matched.length > 0; - if (routeExists) break; + router.push(targetPath).catch(err => { + if (err.name !== 'NavigationDuplicated') { + console.error('路由跳转失败:', err); } - } - - // 如果路由存在,直接跳转 - if (routeExists) { - router.push(targetPath).catch(err => { - if (err.name !== 'NavigationDuplicated') { - console.error('路由跳转失败:', err); - } - }); - } else { - // 如果路由不存在,尝试刷新页面(最后的手段) - console.warn('路由不存在,尝试刷新页面:', targetPath); - // 不刷新页面,而是显示错误提示 - ElMessage.warning(`路由 ${targetPath} 不存在,请刷新缓存后重试`); - } + }); }; const tabsCloseTab = (targetKey) => { tabsStore.removeTab(targetKey); @@ -166,39 +146,17 @@ const closeAll = () => { watch( () => tabsStore.activeTab, (newVal, oldVal) => { - // 如果新值和当前路由路径不同,且不是初始化(oldVal 不为 undefined),才进行跳转 - // 注意:这个watch主要用于处理tab点击切换,菜单点击由handleAsideMenuClick直接处理 if (newVal && oldVal !== undefined && router.currentRoute.value.fullPath !== newVal) { - // 如果切换到首页,关闭其他所有tab if (newVal === defaultDashboardPath) { tabsStore.closeAll(); } - - // 检查路由是否存在 - const routeExists = router.resolve(newVal).matched.length > 0; - - if (routeExists) { - // 路由存在,直接跳转 - nextTick(() => { - router.push(newVal).catch(err => { - if (err.name !== 'NavigationDuplicated') { - console.error('路由跳转失败:', err); - } - }); - }); - } else { - // 如果路由不存在,等待一下再重试(可能是路由还在加载中) - setTimeout(() => { - const retryRouteExists = router.resolve(newVal).matched.length > 0; - if (retryRouteExists && router.currentRoute.value.fullPath !== newVal) { - router.push(newVal).catch(err => { - if (err.name !== 'NavigationDuplicated') { - console.error('路由跳转失败:', err); - } - }); + nextTick(() => { + router.push(newVal).catch(err => { + if (err.name !== 'NavigationDuplicated') { + console.error('路由跳转失败:', err); } - }, 100); - } + }); + }); } }, { flush: 'post' } diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 526e5e3..094867d 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -58,6 +58,8 @@ @@ -66,6 +68,9 @@ +
+ +
@@ -74,7 +79,7 @@ -