feat(projects): 修改路由为根据角色动态添加

This commit is contained in:
‘chen.home’ 2022-08-16 23:30:11 +08:00
parent fd7ef2d3b1
commit 94b22c9453

View File

@ -1,45 +1,18 @@
import { RouteRecordRaw } from 'vue-router'; import { RouteRecordRaw } from 'vue-router';
import { router } from '@/router'; import { router } from '@/router';
import { BasicLayout } from '@/layouts/index'; import { BasicLayout } from '@/layouts/index';
import { useAuthStore } from '@/store';
export async function setDynamicRoutes() { export async function setDynamicRoutes() {
const vueRoutes: RouteRecordRaw[] = [ const authStore = useAuthStore();
{ const vueRootRoute: RouteRecordRaw = {
path: '/test', path: '/test',
name: 'test', name: 'test',
redirect: '/test/test1', redirect: '/test/test1',
component: BasicLayout, component: BasicLayout,
children: [ children: [],
{ };
path: 'test1', const dynamicRoutes = [
name: 'test1',
component: () => import(`@/views/test/test1.vue`),
meta: {
title: '测试1',
icon: 'icon-park-outline:game-three',
requiresAuth: true,
},
},
{
path: 'test2',
name: 'test2',
component: () => import('@/views/test/test2.vue'),
meta: {
title: '测试2',
icon: 'carbon:aperture',
requiresAuth: true,
},
},
{
path: 'test3',
name: 'test3',
component: () => import('@/views/test/test3.vue'),
meta: {
title: '测试3',
icon: 'icon-park-outline:music-list',
requiresAuth: true,
},
},
{ {
path: '/dashboard/workbench', path: '/dashboard/workbench',
name: 'workbench', name: 'workbench',
@ -48,6 +21,7 @@ export async function setDynamicRoutes() {
title: '工作台', title: '工作台',
icon: 'icon-park-outline:music-list', icon: 'icon-park-outline:music-list',
requiresAuth: true, requiresAuth: true,
role: ['admin'],
}, },
}, },
{ {
@ -58,12 +32,46 @@ export async function setDynamicRoutes() {
title: '控制页', title: '控制页',
icon: 'icon-park-outline:music-list', icon: 'icon-park-outline:music-list',
requiresAuth: true, requiresAuth: true,
role: ['admin'],
}, },
}, },
], {
path: '/test/test1',
name: 'test1',
component: () => import(`@/views/test/test1.vue`),
meta: {
title: '测试1',
icon: 'icon-park-outline:game-three',
requiresAuth: true,
role: ['admin'],
},
},
{
path: '/test/test2',
name: 'test2',
component: () => import('@/views/test/test2.vue'),
meta: {
title: '测试2',
icon: 'carbon:aperture',
requiresAuth: true,
role: ['admin'],
},
},
{
path: '/test/test3',
name: 'test3',
component: () => import('@/views/test/test3.vue'),
meta: {
title: '测试3',
icon: 'icon-park-outline:music-list',
requiresAuth: true,
role: ['admin'],
},
}, },
]; ];
vueRoutes.forEach((route) => { // 根据角色过滤后的插入根路由中
router.addRoute(route); vueRootRoute.children = dynamicRoutes.filter((route) => {
return route.meta.role.includes(authStore.userInfo.role);
}); });
router.addRoute(vueRootRoute);
} }