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

🧐[问题 | question] I have two microfrontends using ant design pro, how can i merge the child's menus to the parents menu . #10921

Closed
ngechu opened this issue Aug 28, 2023 · 4 comments
Labels

Comments

@ngechu
Copy link

ngechu commented Aug 28, 2023

🧐 问题描述 | Problem description

💻 示例代码 | Sample code

Am using "umi": "^3.5.0" and "qiankun": "^2.10.11",

多谢

@ngechu
Copy link
Author

ngechu commented Sep 21, 2023

Any help on this will be highly appreciated.
对此的任何帮助将不胜感激。

@chenshuai2144
Copy link
Collaborator

TAutomatically replying with ChatGPT can be attempted, but it cannot be guaranteed to be completely accurate and may not fully address all issues. Please feel free to reply if you have any further questions or concerns.
此回复基于 ChatGPT 自动生成,可以尝试下方案,官方人员会在一定时间后继续继续处理。

🎯 解决方案 | Solution

To merge the child's menus into the parent's menu without hardcoding the routes, you can follow these steps:

  1. Define an API or configuration file in both microfrontends to expose the menu data. This can be a simple JSON file or an API endpoint that returns the menu data.

  2. In your parent application, retrieve the menu data from both microfrontends using the appropriate API or by importing the JSON files.

  3. Merge the menu data from both microfrontends into a single menu structure. You can use JavaScript array manipulation methods like concat(), filter(), map(), or the spread operator (...) to achieve this.

Here's an example of how you can merge the menu data:

// Import menu data from both microfrontends
import microfrontend1Menu from 'microfrontend1/menu';
import microfrontend2Menu from 'microfrontend2/menu';

// Merge the menus
const mergedMenu = [...microfrontend1Menu, ...microfrontend2Menu];

// Render the merged menu in your parent application
<Menu>
  {mergedMenu.map((item) => (
    <MenuItem key={item.key} path={item.path}>
      {item.title}
    </MenuItem>
  ))}
</Menu>

By merging the menu data from both microfrontends, you can dynamically generate the parent menu in your application without hardcoding the routes. This allows you to easily add or remove menus from either microfrontend without having to make changes to the parent application.

Remember to adjust the code based on your specific project structure and requirements.

📚 相关文档 | Related documentation

I hope this helps! Let me know if you have any further questions.

@chenshuai2144
Copy link
Collaborator

To merge the child's menus from two micro frontends into the parent's menu without hard coding the routes, you can use the ProLayout component from Ant Design Pro.

First, make sure each route in your configuration has a name field. This will be used by ProLayout to render the menu.

import { defineConfig } from "umi";

export default defineConfig({
  routes: [
-    { path: "/", component: "index" },
+    { path: "/", component: "index", name: "home" },
-    { path: "/docs", component: "docs" },
+    { path: "/docs", component: "docs", name: "docs" },
-    { path: "/products", component: "products" },
+    { path: "/products", component: "products", name: "products" },
  ],
  plugins: ["@umijs/plugins/dist/react-query"],
  reactQuery: {},
  npmClient: "pnpm",
});

Next, update the src/layouts/index.tsx file with the following code:

import { ProLayout } from '@ant-design/pro-layout';
import { Link, Outlet, useAppData, useLocation } from 'umi';

export default function Layout() {
  const { clientRoutes } = useAppData();
  const location = useLocation();
  return (
    <ProLayout
      route={clientRoutes[0]}
      location={location}
      title="Umi x Ant Design"
      menuItemRender={(menuItemProps, defaultDom) => {
        if (menuItemProps.isUrl || menuItemProps.children) {
          return defaultDom;
        }
        if (menuItemProps.path && location.pathname !== menuItemProps.path) {
          return (
            <Link to={menuItemProps.path} target={menuItemProps.target}>
              {defaultDom}
            </Link>
          );
        }
        return defaultDom;
      }}
    >
      <Outlet />
    </ProLayout>
  );
}

In this code, we use useAppData to retrieve the global client routes clientRoutes and pass clientRoutes[0] to ProLayout as the route prop. We also use useLocation() to get the current location information and pass it to ProLayout. Additionally, we customize the menuItemRender method to handle menu item click events and render the appropriate links.

Finally, you can delete the src/layouts/index.less file to keep your project files clean.

After making these changes, the menus from both micro frontends should appear merged in the parent's menu.

@ngechu
Copy link
Author

ngechu commented Jan 3, 2024

Thanks alot @chenshuai2144

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

No branches or pull requests

2 participants