Compare commits
No commits in common. "0aa25cead21b141b92778875fb2c5e1e8a4cf19d" and "a1f04f94ec5d12c4e3d547810ed966680c81c6a0" have entirely different histories.
0aa25cead2
...
a1f04f94ec
@ -130,7 +130,7 @@ function addDynamicRoutes(menus) {
|
||||
path: "/",
|
||||
name: "Main",
|
||||
component: () => import("@/views/Main.vue"),
|
||||
redirect: "/home",
|
||||
redirect: "/dashboard",
|
||||
meta: { requiresAuth: true },
|
||||
children: [...staticMainChildren, ...dynamicRoutes] // 合并静态和动态子路由
|
||||
});
|
||||
@ -190,17 +190,8 @@ router.beforeEach(async (to, from, next) => {
|
||||
}
|
||||
|
||||
if (!dynamicRoutesAdded) {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
* 通用的别名路径解析工具
|
||||
* 用于在动态导入时解析 @ 别名路径
|
||||
*/
|
||||
import { h } from 'vue';
|
||||
|
||||
// 使用 import.meta.glob 预加载所有组件
|
||||
const viewsModules = import.meta.glob('../views/**/*.vue');
|
||||
|
||||
@ -119,11 +119,31 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果路由存在,直接跳转
|
||||
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);
|
||||
@ -146,10 +166,19 @@ 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') {
|
||||
@ -157,6 +186,19 @@ watch(
|
||||
}
|
||||
});
|
||||
});
|
||||
} 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
},
|
||||
{ flush: 'post' }
|
||||
|
||||
@ -58,8 +58,6 @@
|
||||
</label>
|
||||
<div class="action-links">
|
||||
<a class="forget-link" @click.prevent="goForget">忘记密码?</a>
|
||||
<span> | </span>
|
||||
<a class="forget-link" @click.prevent="clearCache">清除缓存</a>
|
||||
</div>
|
||||
</div>
|
||||
<transition name="fade">
|
||||
@ -68,9 +66,6 @@
|
||||
<button class="login-btn" @click="handleLogin" :disabled="loading">
|
||||
{{ loading ? "登录中..." : "登 录" }}
|
||||
</button>
|
||||
<div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- 背景光效 -->
|
||||
@ -79,7 +74,7 @@
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
<script setup>
|
||||
import { ref, onMounted, nextTick } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
@ -425,23 +420,6 @@ onMounted(() => {
|
||||
|
||||
const goRegister = () => router.push("/register");
|
||||
const goForget = () => router.push("/forget");
|
||||
|
||||
const clearCache = async () => {
|
||||
try {
|
||||
await ElMessageBox.confirm("确定要清除本地缓存吗?", "提示", {
|
||||
confirmButtonText: "确定",
|
||||
cancelButtonText: "取消",
|
||||
type: "warning",
|
||||
});
|
||||
localStorage.clear();
|
||||
sessionStorage.clear();
|
||||
ElMessage.success("缓存已清除");
|
||||
// 刷新页面重新初始化
|
||||
window.location.reload();
|
||||
} catch {
|
||||
// 用户取消,不做任何操作
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user