feat: 菜单添加图标

This commit is contained in:
2025-10-30 16:32:03 +08:00
parent 2ad04594fe
commit 85e1696101

View File

@@ -1,6 +1,6 @@
import * as IconPark from "@icon-park/react"; import * as IconPark from '@icon-park/react';
import { Spin } from "antd"; import { Spin } from 'antd';
import React from "react"; import React from 'react';
// 动态构造 iconMap // 动态构造 iconMap
const iconMap: Record<string, React.ReactNode> = {}; const iconMap: Record<string, React.ReactNode> = {};
@@ -10,8 +10,8 @@ const allIcons = Object.keys(IconPark);
allIcons.forEach((iconName) => { allIcons.forEach((iconName) => {
// 排除不需要的属性(如默认导出等) // 排除不需要的属性(如默认导出等)
if ( if (
iconName !== "default" && iconName !== 'default' &&
typeof IconPark[iconName as keyof typeof IconPark] === "function" typeof IconPark[iconName as keyof typeof IconPark] === 'function'
) { ) {
const IconComponent = IconPark[ const IconComponent = IconPark[
iconName as keyof typeof IconPark iconName as keyof typeof IconPark
@@ -19,7 +19,8 @@ allIcons.forEach((iconName) => {
iconMap[iconName] = <IconComponent theme="outline" size="16" />; iconMap[iconName] = <IconComponent theme="outline" size="16" />;
} }
}); });
import type { MenuVO } from "@/services/system/menu";
import type { MenuVO } from '@/services/system/menu';
export const loopMenuItem = (menus: MenuVO[], pId: number | string): any[] => { export const loopMenuItem = (menus: MenuVO[], pId: number | string): any[] => {
return menus.map((item) => { return menus.map((item) => {
@@ -28,7 +29,7 @@ export const loopMenuItem = (menus: MenuVO[], pId: number | string): any[] => {
// 防止配置了路由,但本地暂未添加对应的页面,产生的错误 // 防止配置了路由,但本地暂未添加对应的页面,产生的错误
Component = React.lazy(() => { Component = React.lazy(() => {
const importComponent = () => import(`@/pages/${item.component}`); const importComponent = () => import(`@/pages/${item.component}`);
const import404 = () => import("@/pages/404"); const import404 = () => import('@/pages/404');
return importComponent().catch(import404); return importComponent().catch(import404);
}); });
} }
@@ -36,18 +37,23 @@ export const loopMenuItem = (menus: MenuVO[], pId: number | string): any[] => {
const routeItem: any = { const routeItem: any = {
path: item.path, path: item.path,
name: item.name, name: item.name,
icon: "",
id: item.id, id: item.id,
icon: '',
parentId: pId, parentId: pId,
hideInMenu: !item.visible, hideInMenu: !item.visible,
children: [], children: [],
}; };
// 如果菜单项有图标且图标存在于图标映射中,则添加图标
if (item.icon && iconMap[item.icon]) {
routeItem.icon = iconMap[item.icon];
}
// 只有当 Component 存在时才添加 element 属性 // 只有当 Component 存在时才添加 element 属性
if (Component) { if (Component) {
routeItem.element = ( routeItem.element = (
<React.Suspense <React.Suspense
fallback={<Spin style={{ width: "100%", height: "100%" }} />} fallback={<Spin style={{ width: '100%', height: '100%' }} />}
> >
<Component /> <Component />
</React.Suspense> </React.Suspense>