feat: 添加 vite-plugin-vue-meta-layouts 插件,优化路由菜单项生成逻辑
Some checks failed
/ playwright (push) Waiting to run
/ depcheck (push) Waiting to run
/ build-and-deploy-to-vercel (push) Has been cancelled

This commit is contained in:
严浩
2024-12-26 15:53:53 +08:00
parent d18799e8cb
commit 4e3633d7ea
6 changed files with 46 additions and 22 deletions

View File

@ -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>

View File

@ -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';