feat: 优化菜单项,添加路由名称和标题支持,更新首页元信息
All checks were successful
/ playwright (push) Successful in 1m21s
/ build-and-deploy-to-vercel (push) Successful in 1m24s
/ depcheck (push) Successful in 1m2s

This commit is contained in:
严浩
2025-01-08 17:41:36 +08:00
parent 943eac62df
commit 83117bbf4b
3 changed files with 11 additions and 5 deletions

View File

@ -4,13 +4,15 @@ import type { MenuItem } from 'primevue/menuitem';
const router = useRouter();
type MenuItemWithRoute = MenuItem & { routeName?: string };
const menuItems = computed(() => {
let flatArray: MenuItem[] = createGetRoutes(router)()
let flatArray: MenuItemWithRoute[] = createGetRoutes(router)()
.filter((route) => !route.path.includes('/:'))
.filter((route) => !route.meta.hidden)
.map((route) => ({
id: route.path,
label: `${(route.name as string) || route.path}`,
label: route.meta.title || `${(route.name as string) || route.path}`,
routeName: route.name as string,
}));
flatArray = flatArray.map((item, index) => {
@ -52,13 +54,13 @@ const menuItems = computed(() => {
const tree = arrayToTree(flatArray.concat(groupItems), { id: 'id', parentId: 'parentId', rootId: '_ROOT_' });
// 递归把 children 改为 items
function _convertChildrenToItems(tree: MenuItem[]): MenuItem[] {
function _convertChildrenToItems(tree: MenuItemWithRoute[]) {
return tree.map((item) => {
if (item.children.length) {
item.items = _convertChildrenToItems(item.children);
} else {
item.command = (event) => {
router.push({ name: item.label as never });
router.push({ name: item.routeName as never });
};
}
delete item.children;

View File

@ -1,7 +1,7 @@
<script setup lang="tsx">
const VITE_BUILD_COMMIT = import.meta.env.VITE_BUILD_COMMIT;
import { routes } from 'vue-router/auto-routes';
definePage({ meta: { title: '首页' } });
useHead({
// Titles
title: 'Hello World',

View File

@ -46,5 +46,9 @@ declare module 'vue-router' {
* @description 是否在菜单中隐藏
*/
hidden?: boolean;
/**
* @description 菜单标题
*/
title?: string;
}
}