Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义布局及路由问题 #11078

Closed
wuchencm opened this issue Dec 5, 2023 · 4 comments
Closed

自定义布局及路由问题 #11078

wuchencm opened this issue Dec 5, 2023 · 4 comments

Comments

@wuchencm
Copy link

wuchencm commented Dec 5, 2023

目前使用的是ant design pro v6的版本,想实现一个自定义布局的功能

这是我的自定义布局的代码
`
const CustomLayout: React.FC = (props) => {

// const menuDataRender = () => [
// {
// name:'概览',
// path:'/appstack/app/:appName/overview',
// component: './AppStack/index.tsx',
// },
// {
// name:'研发流程',
// path:'/appstack/app/:appName/workflow',
// component: './AppStack/index.tsx',
// },
// {
// name:'变更',
// path:'/appstack/app/:appName/changes',
// component: './AppStack/index.tsx',
// },
// {
// name:'环境',
// path:'/appstack/app/:appName/envs',
// component: './AppStack/index.tsx',
// },
// ];

return (
    <ProLayout
        {...props}
        logo={false}
        title={false}
        layout='mix'
        headerRender={false}
        // menuDataRender={menuDataRender}
        // fixSiderbar={true}
        // menuItemRender={(menuItemProps, defaultDom) => {
        //     if (menuItemProps.isUrl || menuItemProps.children || !menuItemProps.path) {
        //         return defaultDom;
        //     }
        //     return <Link to={menuItemProps.path}>{defaultDom}</Link>;
        // }} // 使用自定义的菜单数据渲染
    >
        {props.children}
      adadad
    </ProLayout>
);

}`
这是我的routes.ts 配置:
image
遇到的问题是:
image

这个菜单数据在自定义布局文件中配置的话是可以展示的,难道不能接收routes里的数据吗?

@guwan
Copy link

guwan commented Dec 13, 2023

我遇到同样的问题,请问有没有解决这个问题呢?

@guwan
Copy link

guwan commented Dec 13, 2023

@wuchencm 我解决了你这个问题,现在需要使用来代替{props.children}了
<ProLayout > <div> MainLayout page <div> <Outlet/> </div> </div> </ProLayout>

@wuchencm wuchencm reopened this Dec 29, 2023
@wuchencm
Copy link
Author

@wuchencm 我解决了你这个问题,现在需要使用来代替{props.children}了 <ProLayout > <div> MainLayout page <div> <Outlet/> </div> </div> </ProLayout>

是的,Outlet确实可以,但是怎么才能将routes里定义的路由信息呢

@chenshuai2144
Copy link
Collaborator

根据你提供的代码和问题描述,看起来你希望在自定义布局文件中使用routes中定义的菜单数据。但是在你的代码中,将menuDataRender注释掉了,这导致自定义布局文件没有菜单数据进行渲染。

要解决此问题,你可以取消注释menuDataRender函数,并根据routes中的数据来返回菜单数据。在你的menuDataRender函数中,你可以使用routes中的数据来动态生成菜单数据,如下所示:

const menuDataRender = () => {
  // 获取routes配置中的数据
  const routes = [
    {
      path: "/",
      name: "首页",
      icon: "home",
      routes: [
        {
          path: "/dashboard",
          name: "Dashboard",
        },
        // 其他路由配置...
      ],
    },
    // 其他顶级路由配置...
  ];

  // 处理路由数据,生成菜单数据
  const formatMenuData = (routes) => {
    return routes.map((route) => {
      const { path, name, icon, routes: subRoutes } = route;
      const menuData = {
        path,
        name,
        icon,
      };

      // 如果有子路由,递归处理子路由
      if (subRoutes && subRoutes.length > 0) {
        menuData.routes = formatMenuData(subRoutes);
      }

      return menuData;
    });
  };

  return formatMenuData(routes);
};

然后在ProLayout组件中使用menuDataRender函数来渲染菜单数据,取消注释相关代码,如下所示:

<ProLayout
  {...props}
  logo={false}
  title={false}
  layout="mix"
  headerRender={false}
  menuDataRender={menuDataRender}
>
  {props.children}
</ProLayout>

这样就可以让自定义布局文件根据routes中的数据来渲染菜单了。希望这个回答对你有所帮助!如果还有其他问题,请随时提问。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants