fix(projects): 修复路由重定向问题

This commit is contained in:
‘chen.home’ 2022-08-15 23:17:10 +08:00
parent 8e1008d004
commit 5b26d6c58e
4 changed files with 18 additions and 15 deletions

View File

@ -6,13 +6,13 @@ import { constantRoutes } from '../routes';
export async function setDynamicRoutes() { export async function setDynamicRoutes() {
const vueRoutes: RouteRecordRaw[] = [ const vueRoutes: RouteRecordRaw[] = [
{ {
path: '/', path: '/test',
name: 'root', name: 'test',
redirect: '/test/test1', redirect: '/test/test1',
component: BasicLayout, component: BasicLayout,
children: [ children: [
{ {
path: '/test/test1', path: 'test1',
name: 'test1', name: 'test1',
component: () => import(`@/views/test/test1.vue`), component: () => import(`@/views/test/test1.vue`),
meta: { meta: {
@ -22,7 +22,7 @@ export async function setDynamicRoutes() {
}, },
}, },
{ {
path: '/test/test2', path: 'test2',
name: 'test2', name: 'test2',
component: () => import('@/views/test/test2.vue'), component: () => import('@/views/test/test2.vue'),
meta: { meta: {
@ -32,7 +32,7 @@ export async function setDynamicRoutes() {
}, },
}, },
{ {
path: '/test/test3', path: 'test3',
name: 'test3', name: 'test3',
component: () => import('@/views/test/test3.vue'), component: () => import('@/views/test/test3.vue'),
meta: { meta: {

View File

@ -26,8 +26,11 @@ export async function createPermissionGuard(
} }
// 有登录但是没有路由,初始化路由、侧边菜单等 // 有登录但是没有路由,初始化路由、侧边菜单等
await routeStore.initAuthRoute();
await setDynamicRoutes(); await setDynamicRoutes();
await routeStore.initAuthRoute();
// 动态路由加载完回到根路由
next({ name: 'root' });
return false;
} }
// 权限路由已经加载仍然未找到重定向到not-found // 权限路由已经加载仍然未找到重定向到not-found
// if (to.name === 'not-found-page') { // if (to.name === 'not-found-page') {

View File

@ -50,9 +50,8 @@ export const useAuthStore = defineStore('auth-store', {
// 等待数据写入完成 // 等待数据写入完成
const catchSuccess = await this.catchUserInfo(data); const catchSuccess = await this.catchUserInfo(data);
// 初始化侧边菜单 // 初始化侧边菜单
const { setMenus, setUserRoutes } = useRouteStore(); const { initAuthRoute } = useRouteStore();
await setUserRoutes(data.userRoutes); await initAuthRoute();
await setMenus();
// 登录写入信息成功 // 登录写入信息成功
if (catchSuccess) { if (catchSuccess) {

View File

@ -1,7 +1,7 @@
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { renderIcon, getUserInfo } from '@/utils'; import { renderIcon, getUserInfo } from '@/utils';
import { MenuOption, radioGroupProps } from 'naive-ui'; import { MenuOption, radioGroupProps } from 'naive-ui';
import { setDynamicRoutes } from '@/router/guard/dynamic'; // import { setDynamicRoutes } from '@/router/guard/dynamic';
interface RoutesStatus { interface RoutesStatus {
isInitAuthRoute: boolean; isInitAuthRoute: boolean;
@ -17,11 +17,12 @@ export const useRouteStore = defineStore('route-store', {
}; };
}, },
actions: { actions: {
async setMenus() { async setUserRoutes() {
this.menus = this.transformAuthRoutesToMenus(this.userRoutes); this.userRoutes = getUserInfo().userRoutes;
}, },
async setUserRoutes(routes: any) { async setMenus() {
this.userRoutes = routes; this.setUserRoutes();
this.menus = this.transformAuthRoutesToMenus(this.userRoutes);
}, },
// 将返回的路由表渲染成侧边栏 // 将返回的路由表渲染成侧边栏
transformAuthRoutesToMenus(userRoutes: Auth.UserInfoPermissions[]): MenuOption[] { transformAuthRoutesToMenus(userRoutes: Auth.UserInfoPermissions[]): MenuOption[] {
@ -60,7 +61,7 @@ export const useRouteStore = defineStore('route-store', {
async initAuthRoute() { async initAuthRoute() {
await this.setMenus(); await this.setMenus();
await setDynamicRoutes(); // await setDynamicRoutes();
this.isInitAuthRoute = true; this.isInitAuthRoute = true;
}, },
}, },