diff --git a/src/router/guard/dynamic.ts b/src/router/guard/dynamic.ts index 2d6065e..bc9fa88 100644 --- a/src/router/guard/dynamic.ts +++ b/src/router/guard/dynamic.ts @@ -6,13 +6,13 @@ import { constantRoutes } from '../routes'; export async function setDynamicRoutes() { const vueRoutes: RouteRecordRaw[] = [ { - path: '/', - name: 'root', + path: '/test', + name: 'test', redirect: '/test/test1', component: BasicLayout, children: [ { - path: '/test/test1', + path: 'test1', name: 'test1', component: () => import(`@/views/test/test1.vue`), meta: { @@ -22,7 +22,7 @@ export async function setDynamicRoutes() { }, }, { - path: '/test/test2', + path: 'test2', name: 'test2', component: () => import('@/views/test/test2.vue'), meta: { @@ -32,7 +32,7 @@ export async function setDynamicRoutes() { }, }, { - path: '/test/test3', + path: 'test3', name: 'test3', component: () => import('@/views/test/test3.vue'), meta: { diff --git a/src/router/guard/permission.ts b/src/router/guard/permission.ts index 5db1ee9..96b0158 100644 --- a/src/router/guard/permission.ts +++ b/src/router/guard/permission.ts @@ -26,8 +26,11 @@ export async function createPermissionGuard( } // 有登录但是没有路由,初始化路由、侧边菜单等 - await routeStore.initAuthRoute(); await setDynamicRoutes(); + await routeStore.initAuthRoute(); + // 动态路由加载完回到根路由 + next({ name: 'root' }); + return false; } // 权限路由已经加载,仍然未找到,重定向到not-found // if (to.name === 'not-found-page') { diff --git a/src/store/modules/auth.ts b/src/store/modules/auth.ts index c31534a..4a0d747 100644 --- a/src/store/modules/auth.ts +++ b/src/store/modules/auth.ts @@ -50,9 +50,8 @@ export const useAuthStore = defineStore('auth-store', { // 等待数据写入完成 const catchSuccess = await this.catchUserInfo(data); // 初始化侧边菜单 - const { setMenus, setUserRoutes } = useRouteStore(); - await setUserRoutes(data.userRoutes); - await setMenus(); + const { initAuthRoute } = useRouteStore(); + await initAuthRoute(); // 登录写入信息成功 if (catchSuccess) { diff --git a/src/store/modules/route.ts b/src/store/modules/route.ts index facbaef..81b8106 100644 --- a/src/store/modules/route.ts +++ b/src/store/modules/route.ts @@ -1,7 +1,7 @@ import { defineStore } from 'pinia'; import { renderIcon, getUserInfo } from '@/utils'; import { MenuOption, radioGroupProps } from 'naive-ui'; -import { setDynamicRoutes } from '@/router/guard/dynamic'; +// import { setDynamicRoutes } from '@/router/guard/dynamic'; interface RoutesStatus { isInitAuthRoute: boolean; @@ -17,11 +17,12 @@ export const useRouteStore = defineStore('route-store', { }; }, actions: { - async setMenus() { - this.menus = this.transformAuthRoutesToMenus(this.userRoutes); + async setUserRoutes() { + this.userRoutes = getUserInfo().userRoutes; }, - async setUserRoutes(routes: any) { - this.userRoutes = routes; + async setMenus() { + this.setUserRoutes(); + this.menus = this.transformAuthRoutesToMenus(this.userRoutes); }, // 将返回的路由表渲染成侧边栏 transformAuthRoutesToMenus(userRoutes: Auth.UserInfoPermissions[]): MenuOption[] { @@ -60,7 +61,7 @@ export const useRouteStore = defineStore('route-store', { async initAuthRoute() { await this.setMenus(); - await setDynamicRoutes(); + // await setDynamicRoutes(); this.isInitAuthRoute = true; }, },