Ant-design-pro: 动态菜单显示问题🐛[BUG]

Created on 27 Jul 2020  ·  1Comment  ·  Source: ant-design/ant-design-pro

🐛 bug 描述

我菜单是使用动态加载出来 ,不过发现个问题。就是动态加载出来的时候 。菜单 没有自动展开并点亮,是全都闭合的
image

然后我把请求的数据直接写死在代码中。就没有问题
image

📷 复现步骤

image

🏞 期望结果

image

💻 复现代码

/**
 * Ant Design Pro v4 use `@ant-design/pro-layout` to handle Layout.
 * You can view component api by:
 * https://github.com/ant-design/ant-design-pro-layout
 */
import ProLayout, {
  MenuDataItem,
  BasicLayoutProps as ProLayoutProps,
  Settings,
} from '@ant-design/pro-layout';
import React, { useEffect, useState } from 'react';
import { Link } from 'umi';
import { Dispatch } from 'redux';
import { connect } from 'dva';
import { Result, Button } from 'antd';
import Authorized from '@/utils/Authorized';
import RightContent from '@/components/GlobalHeader/RightContent';
import { ConnectState } from '@/models/connect';
import {  getAuthorityFromRouter } from '@/utils/utils';
import logo from '../assets/logo.svg';
import Iconfont from "../components/Iconfont";

const noMatch = (
  <Result
    status={403}
    title="403"
    subTitle="Sorry, you are not authorized to access this page."
    extra={
      <Button type="primary">
        <Link to="/account/login">Go Login</Link>
      </Button>
    }
  />
);
export interface BasicLayoutProps extends ProLayoutProps {
  breadcrumbNameMap: {
    [path: string]: MenuDataItem;
  };
  route: ProLayoutProps['route'] & {
    authority: string[];
  };
  settings: Settings;
  dispatch: Dispatch;
}
export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
  breadcrumbNameMap: {
    [path: string]: MenuDataItem;
  };
};
/**
 * use Authorized check all menu item
 */

const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] =>
  menuList.map(item => {
    const localItem = { ...item, children: item.children ? menuDataRender(item.children) : [] };
    return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  });


const BasicLayout: React.FC<BasicLayoutProps> = props => {
  const {
    dispatch,
    children,
    settings,
    location = {
      pathname: '/',
    },
  } = props;
  /**
   * constructor
   */
  const [menuData, setMenuData] = useState([]);

  useEffect(() => {
    if (dispatch) {
      dispatch({
        type: 'admin/getLogin',
      });
    }
    // 这里是一个演示用法
    // 真实项目中建议使用 dva dispatch 或者 umi-request
    fetch('/api/admin/getMenu')
      .then(response => response.json())
      .then(data => {
        setMenuData(data || []);
      });
  }, []);
  /**
   * init variables
   */

  const handleMenuCollapse = (payload: boolean): void => {
    if (dispatch) {
      dispatch({
        type: 'global/changeLayoutCollapsed',
        payload,
      });
    }
  }; // get children authority

  const authorized = getAuthorityFromRouter(props.route.routes, location.pathname || '/') || {
    authority: undefined,
  };
  return (
    <ProLayout
      logo={logo}
      // formatMessage={formatMessage}
      menuHeaderRender={(logoDom, titleDom) => (
        <Link to="/">
          <span style={{ paddingLeft: '10px' }}>
            {titleDom}
          </span>
        </Link>
      )}
      onCollapse={handleMenuCollapse}
      menuItemRender={(menuItemProps, defaultDom) => {
        if (menuItemProps.isUrl || menuItemProps.children || !menuItemProps.path) {
          return defaultDom;
        }

        return <Link to={menuItemProps.path}>
          <Iconfont type={menuItemProps.icon} style={{ fontSize: '14px', marginRight: '10px' }} />
          <span>{menuItemProps.name}</span>
        </Link>;
      }}
      breadcrumbRender={(routers = []) => [
        {
          path: '/',
          breadcrumbName: '首页',
        },
        ...routers,
      ]}
      itemRender={(route, params, routes, paths) => {
        const first = routes.indexOf(route) === 0;
        return first ? (
          <Link to={paths.join('/')}>{route.breadcrumbName}</Link>
        ) : (
            <span>{route.breadcrumbName}</span>
          );
      }}
      footerRender={false}
      menuDataRender={() => menuData}
      rightContentRender={() => <RightContent />}
      {...props}
      {...settings}
    >
      <Authorized authority={authorized!.authority} noMatch={noMatch}>
        {children}
      </Authorized>
    </ProLayout>
  );
};

export default connect(({ global, settings }: ConnectState) => ({
  collapsed: global.collapsed,
  settings,
}))(BasicLayout);

© 版本信息

  • Ant Design Pro 版本: [e.g. 4.0.0]
  • umi 版本3.2.0
  • 浏览器环境Google Chrome 81.0.4044.113
  • 开发环境 window 10

🚑 其他信息

Inactive 🛑 bug

Most helpful comment

如下用法即可解决问题:
{menuData && menuData.length > 0 && ( <ProLayout </ProLayout> )}

>All comments

如下用法即可解决问题:
{menuData && menuData.length > 0 && ( <ProLayout </ProLayout> )}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yaoleiroyal picture yaoleiroyal  ·  3Comments

kaoding picture kaoding  ·  3Comments

zhuanglong picture zhuanglong  ·  3Comments

yjz1004 picture yjz1004  ·  3Comments

gaoqiang19514 picture gaoqiang19514  ·  3Comments