优化多级菜单,路由注册

dianlixunjian
刘妍 2024-06-24 11:06:16 +08:00
parent 63867b31a9
commit 23c0a4437e
3 changed files with 52 additions and 45 deletions

View File

@ -168,6 +168,53 @@ export const usePermissionStore = defineStore({
}
return newPath;
};
const childRoute = (childData) => {
const modules = import.meta.glob('../../views/**/**/**/index.vue');
childData.forEach((element) => {
// 菜单里配置带参数,但没有单独的主路由,先注册主路由
if (element.item.status == 1 && element.item.url.indexOf('@') > 0) {
const path = element.item.url.substring(0, element.item.url.indexOf('@'));
router.addRoute('Root', {
path: path,
name: element.item.code,
meta: {
title: element.item.name,
icon: element.item.iconName,
hideChildrenInMenu: true,
elements: element.item.elements,
},
component: LAYOUT,
children: [
{
path: path + '/:id',
name: element.item.url.replaceAll('/', ''),
meta: {
title: element.item.name,
icon: element.item.iconName,
elements: element.item.elements,
},
component: modules['../../views/demo' + path + '/index.vue'],
},
],
});
} else if (element.item.code && element.item.status == 1) {
router.addRoute(element.item.code, {
path: element.item.url,
name: element.item.url.replaceAll('/', ''),
meta: {
title: element.item.name,
icon: element.item.iconName,
elements: element.item.elements,
},
// component: () => import('../../views/demo' + element.item.url + '/index.vue')
component: modules['../../views/demo' + element.item.url + '/index.vue'],
});
}
if (element.children && element.children.length > 0) {
childRoute(element.children);
}
});
};
// 注册路由
const registeredRoute = (data) => {
const modules = import.meta.glob('../../views/**/**/**/index.vue');
@ -185,7 +232,7 @@ export const usePermissionStore = defineStore({
});
} else {
// 正常菜单
if (data.children.length == 0 && data.item.parentId == 0 && data.item.status == 1) {
if (data.children.length == 0 && data.item.status == 1) {
// 没有子菜单
if (data.item.url.indexOf('@') > 0) {
//给带参数的路由先注册主路由
@ -257,51 +304,11 @@ export const usePermissionStore = defineStore({
: modules['../../views/demo' + data.item.url + '/index.vue'],
});
if (data.children && data.children.length > 0) {
data.children.forEach((element) => {
// 菜单里配置带参数,但没有单独的主路由,先注册主路由
if (element.item.status == 1 && element.item.url.indexOf('@') > 0) {
const path = element.item.url.substring(0, element.item.url.indexOf('@'));
router.addRoute('Root', {
path: path,
name: element.item.code,
meta: {
title: element.item.name,
icon: element.item.iconName,
hideChildrenInMenu: true,
elements: element.item.elements,
},
component: LAYOUT,
children: [
{
path: path + '/:id',
name: element.item.url.replaceAll('/', ''),
meta: {
title: element.item.name,
icon: element.item.iconName,
elements: element.item.elements,
},
component: modules['../../views/demo' + path + '/index.vue'],
},
],
});
} else if (element.item.code && element.item.status == 1) {
router.addRoute(element.item.code, {
path: element.item.url,
name: element.item.url.replaceAll('/', ''),
meta: {
title: element.item.name,
icon: element.item.iconName,
elements: element.item.elements,
},
// component: () => import('../../views/demo' + element.item.url + '/index.vue')
component: modules['../../views/demo' + element.item.url + '/index.vue'],
});
}
});
childRoute(data.children);
}
}
}
// console.log(router.getRoutes())
// console.log(router.getRoutes());
};
//通过后端获取菜单

View File

@ -56,7 +56,7 @@
const processId = ref('');
const route = useRoute();
// searchInfo.category = route.query.category;
searchInfo.category = route.params?.id;
searchInfo.category = route.params?.id == 'all' ? '' : route.params?.id;
const [registerTable, { reload }] = useTable({
api: getLoadMyCompletedPage,

View File

@ -75,7 +75,7 @@
const childRecord = ref();
const route = useRoute();
// searchInfo.category = route.query.category;
searchInfo.category = route.params?.id;
searchInfo.category = route.params?.id == 'all' ? '' : route.params?.id;
const [registerTable, { reload }] = useTable({
api: getLoadMyUncompletedPage,
columns: [