更改开发目录
This commit is contained in:
@@ -25,6 +25,14 @@ const router = createRouter({
|
||||
}
|
||||
});
|
||||
|
||||
// 标记动态路由是否已经注册
|
||||
let dynamicRoutesRegistered = false;
|
||||
|
||||
// 重置动态路由注册状态的函数
|
||||
export function resetDynamicRoutes() {
|
||||
dynamicRoutesRegistered = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 路由守卫
|
||||
*/
|
||||
@@ -48,10 +56,20 @@ router.beforeEach(async (to, from) => {
|
||||
|
||||
// 注册动态路由
|
||||
const userStore = useUserStore();
|
||||
if (!userStore.menus) {
|
||||
if (!userStore.menus && !dynamicRoutesRegistered) {
|
||||
const { menus, homePath } = await userStore.fetchUserInfo();
|
||||
if (menus) {
|
||||
router.addRoute(getMenuRoutes(menus, homePath));
|
||||
const menuRoute = getMenuRoutes(menus, homePath);
|
||||
router.addRoute(menuRoute);
|
||||
dynamicRoutesRegistered = true;
|
||||
|
||||
// 只有当访问根路径时才跳转到首页
|
||||
if (to.path === LAYOUT_PATH) {
|
||||
return { path: homePath || '/dashboard', replace: true };
|
||||
}
|
||||
|
||||
// 对于其他路径,只有在路由确实不存在时才跳转
|
||||
// 这避免了已存在页面的不必要跳转
|
||||
return { ...to, replace: true };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user