antd pro V5从服务端请求菜单

要从服务端请求菜单可以在src下的app.tsx中修改运行时配置,menuDataRender或者menu,

在本人测试中如果使用menu项配置可以实现从服务端请求代码,不过locale:false无效,菜单名称还是会从国际化配置文件中读取。使用menuDataRender配置则设置locale:false即可不从国际化配置文件中读取。

注意,以下代码只会修改左侧菜单的现实,至于菜单项和组件的关系仍需要在config/routes.ts中配置。

export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) => {
  return {
    /*menu: {
      // 每当 initialState?.currentUser?.userid 发生修改时重新执行 request
      params: {
        username: initialState?.currentUser?.user.userName,
      },
      request: async (params, defaultMenuData) => {
        // console.log(params, defaultMenuData)
        console.log('customMenuData: ', initialState?.customMenuData)
        const menuData = initialState?.customMenuData;
        const menuDataIcon = menuData?.map(ele => {
          if (ele.iconName) {
            const iconNode: React.ReactNode = iconMapping[ele.iconName];
            if (iconNode) {
              ele.icon = iconNode;
            }
          }
          if (ele.name === '系统管理') {
            console.log('ele?.routes?[0]: ', ele?.routes||[][0]);
            const component = (ele?.routes||[])[0].component;
            // console.log('component:', component)
            (ele?.routes||[])[0].component = dynamic({ loader: () => import(component||''), loading: LoadingComponent})
          }

          // ele?.routes||[][0].component = dynamic({ loader: () => import(ele?.routes||[][0].component||''), loading: LoadingComponent})
          // ele.icon = <BorderLeftOutlined />
          return ele;
        })
        // initialState.currentUser 中包含了所有用户信息
        // const menuData = await fetchMenuData();
        const data = [...defaultMenuData, ...menuDataIcon||[]]
        console.log('menu data: ', data)
        console.log('iconMap: ', iconMap)
        console.log('iconMap: ', iconMap['BorderLeftOutlined'])
        return data;
      },
    },*/
    rightContentRender: () => <RightContent />,
    disableContentMargin: false,
    waterMarkProps: {
      content: initialState?.currentUser?.name,
    },
    footerRender: () => <Footer />,
    onPageChange: () => {
      const { location } = history;
      // 如果没有登录,重定向到 login
      if (!initialState?.currentUser && location.pathname !== loginPath) {
        history.push(loginPath);
      }
    },
    links: isDev
      ? [
          <Link to="/umi/plugin/openapi" target="_blank">
            <LinkOutlined />
            <span>OpenAPI 文档</span>
          </Link>,
          <Link to="/~docs">
            <BookOutlined />
            <span>业务组件文档</span>
          </Link>,
        ]
      : [],
    menuDataRender: (menuItems) => {
      const resMenuData = initialState?.customMenuData||[];
      const menuDataIcon = handleIconAndComponent(resMenuData);
      const data = [...menuDataIcon||[]]
      return data;
    },
    menuHeaderRender: undefined,
    menuItemRender: (menuItemProps, defaultDom) => {
      if (menuItemProps.isUrl || !menuItemProps.path) {
        return defaultDom;
      }
      // 支持二级菜单显示icon
      return (
        <Link to={menuItemProps.path}>
          {menuItemProps.pro_layout_parentKeys
            && menuItemProps.pro_layout_parentKeys.length > 0 &&
            menuItemProps.icon}{defaultDom}
        </Link>
      );
    },
    // 自定义 403 页面
    // unAccessible: <div>unAccessible</div>,
    // 增加一个 loading 的状态
    childrenRender: (children, props) => {
      return (
        <>
          {children}
          {!props.location?.pathname?.includes('/login') && (
            <SettingDrawer
              enableDarkTheme
              settings={initialState?.settings}
              onSettingChange={(settings) => {
                setInitialState((preInitialState) => ({
                  ...preInitialState,
                  settings,
                }));
              }}
            />
          )}
        </>
      );
    },
    ...initialState?.settings,
  };
};

handleIconAndComponent: 处理菜单图标问题

import {SYSTEM} from "@/services/system/typings";
import React from "react";
import * as allIcons from '@ant-design/icons';

export function handleIconAndComponent(routes: SYSTEM.Router[]) {
  return routes.map(ele => {
    if (ele.iconName) {
      const iconNode: React.ReactNode = React.createElement(allIcons[ele.iconName]);
      if (iconNode) {
        ele.icon = iconNode;
      }
    }
    delete ele.component
    if (ele.routes && ele.routes.length > 0) {
      handleIconAndComponent(ele.routes);
    }
    return ele;
  })
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值