-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Comments
Any help on this will be highly appreciated. |
🎯 解决方案 | SolutionTo merge the child's menus into the parent's menu without hardcoding the routes, you can follow these steps:
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. |
To merge the child's menus from two micro frontends into the parent's menu without hard coding the routes, you can use the First, make sure each route in your configuration has a 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 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 Finally, you can delete the After making these changes, the menus from both micro frontends should appear merged in the parent's menu. |
Thanks alot @chenshuai2144 |
🧐 问题描述 | Problem description
💻 示例代码 | Sample code
Am using "umi": "^3.5.0" and "qiankun": "^2.10.11",
多谢
The text was updated successfully, but these errors were encountered: