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

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

View File

@ -1,69 +1,77 @@
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',
name: 'test',
redirect: '/test/test1',
component: BasicLayout,
children: [],
};
const dynamicRoutes = [
{ {
path: '/test', path: '/dashboard/workbench',
name: 'test', name: 'workbench',
redirect: '/test/test1', component: () => import('@/views/dashboard/workbench/index.vue'),
component: BasicLayout, meta: {
children: [ title: '工作台',
{ icon: 'icon-park-outline:music-list',
path: 'test1', requiresAuth: true,
name: 'test1', role: ['admin'],
component: () => import(`@/views/test/test1.vue`), },
meta: { },
title: '测试1', {
icon: 'icon-park-outline:game-three', path: '/dashboard/monitor',
requiresAuth: true, name: 'monitor',
}, component: () => import('@/views/dashboard/monitor/index.vue'),
}, meta: {
{ title: '控制页',
path: 'test2', icon: 'icon-park-outline:music-list',
name: 'test2', requiresAuth: true,
component: () => import('@/views/test/test2.vue'), role: ['admin'],
meta: { },
title: '测试2', },
icon: 'carbon:aperture', {
requiresAuth: true, path: '/test/test1',
}, name: 'test1',
}, component: () => import(`@/views/test/test1.vue`),
{ meta: {
path: 'test3', title: '测试1',
name: 'test3', icon: 'icon-park-outline:game-three',
component: () => import('@/views/test/test3.vue'), requiresAuth: true,
meta: { role: ['admin'],
title: '测试3', },
icon: 'icon-park-outline:music-list', },
requiresAuth: true, {
}, path: '/test/test2',
}, name: 'test2',
{ component: () => import('@/views/test/test2.vue'),
path: '/dashboard/workbench', meta: {
name: 'workbench', title: '测试2',
component: () => import('@/views/dashboard/workbench/index.vue'), icon: 'carbon:aperture',
meta: { requiresAuth: true,
title: '工作台', role: ['admin'],
icon: 'icon-park-outline:music-list', },
requiresAuth: true, },
}, {
}, path: '/test/test3',
{ name: 'test3',
path: '/dashboard/monitor', component: () => import('@/views/test/test3.vue'),
name: 'monitor', meta: {
component: () => import('@/views/dashboard/monitor/index.vue'), title: '测试3',
meta: { icon: 'icon-park-outline:music-list',
title: '控制页', requiresAuth: true,
icon: 'icon-park-outline:music-list', role: ['admin'],
requiresAuth: true, },
},
},
],
}, },
]; ];
vueRoutes.forEach((route) => { // 根据角色过滤后的插入根路由中
router.addRoute(route); vueRootRoute.children = dynamicRoutes.filter((route) => {
return route.meta.role.includes(authStore.userInfo.role);
}); });
router.addRoute(vueRootRoute);
} }