feat: 添加 vite-plugin-vue-meta-layouts 插件,优化路由菜单项生成逻辑
This commit is contained in:
@ -1,36 +1,35 @@
|
||||
<script setup lang="ts">
|
||||
import type { MenuItem } from 'primevue/menuitem';
|
||||
import type { PanelMenuProps } from 'primevue/panelmenu';
|
||||
import { createGetRoutes } from 'virtual:meta-layouts';
|
||||
import type { RouteRecordRaw } from 'vue-router/auto';
|
||||
import { routes } from 'vue-router/auto-routes';
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
// 递归处理路由生成菜单项
|
||||
const generateMenuItems = (routes: RouteRecordRaw[]): PanelMenuProps['model'] => {
|
||||
return routes.map((route, index) => {
|
||||
const menuItem: MenuItem = {
|
||||
label: `${index + 1}. ${(route.name as string) || route.path}`,
|
||||
icon: (route.meta?.icon as string) || 'pi pi-fw pi-home',
|
||||
// url: route.path,
|
||||
command: route.children?.length
|
||||
? undefined
|
||||
: () => {
|
||||
console.debug(`route :>> `, route);
|
||||
router.push(route);
|
||||
},
|
||||
// _route_info_: JSON.parse(JSON.stringify(route)),
|
||||
};
|
||||
const getRoutes = createGetRoutes(router);
|
||||
|
||||
if (route.children && route.children.length > 0) {
|
||||
menuItem.items = generateMenuItems(route.children);
|
||||
}
|
||||
return menuItem;
|
||||
});
|
||||
// 递归处理路由生成菜单项
|
||||
const _generateMenuItems = (routes: RouteRecordRaw[]): PanelMenuProps['model'] => {
|
||||
return [];
|
||||
};
|
||||
|
||||
const cmptItems = computed(() => {
|
||||
return generateMenuItems(routes);
|
||||
return getRoutes()
|
||||
.filter((route) => !route.path.includes('/:'))
|
||||
.map((route, index) => {
|
||||
return {
|
||||
label: `${index}. ${(route.name as string) || route.path}`,
|
||||
icon: (route.meta?.icon as string) || 'pi pi-fw pi-home',
|
||||
// url: route.path,
|
||||
command: route.children?.length
|
||||
? undefined
|
||||
: () => {
|
||||
console.debug(`route :>> `, route);
|
||||
router.push(route);
|
||||
},
|
||||
} satisfies MenuItem;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders';
|
||||
import { setupLayouts, createGetRoutes } from 'virtual:generated-layouts';
|
||||
import { createGetRoutes, setupLayouts } from 'virtual:meta-layouts';
|
||||
// import { setupLayouts, createGetRoutes } from 'virtual:generated-layouts';
|
||||
import { createRouter, createWebHistory } from 'vue-router/auto';
|
||||
import { handleHotUpdate, routes } from 'vue-router/auto-routes';
|
||||
|
||||
|
Reference in New Issue
Block a user