处理嵌套路由与跨应用导航
学习如何协调由各个微前端负责的嵌套路由,以及如何在不同 MFE 之间无缝导航。
处理嵌套路由与跨应用导航 是 CoddyKit 上的免费 Micro Frontends Architecture with Module Federation 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Micro Frontends Architecture with Module Federation 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Micro Frontends Architecture with Module Federation 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Nested Routing Challenge
In federated apps, the shell owns top-level routes while each micro frontend owns its own internal (nested) routes. Coordinating the two is the heart of MFE navigation.
Route Ownership
A clear rule helps: the shell maps a route prefix to an MFE, and everything below that prefix belongs to the MFE.
/products/* -> product MFE
/cart/* -> cart MFE
/account/* -> account MFEMounting an MFE at a Prefix
The shell renders the MFE for a prefix and lets it handle the remaining path segment.
<Route path="/products/*" element={<ProductApp />} />Nested Routes Inside an MFE
The MFE defines its own router scoped to its prefix, so it controls its sub-pages without the shell knowing about them.
<Routes>
<Route path="/" element={<List />} />
<Route path=":id" element={<Detail />} />
</Routes>basename for Scoped Routers
Give the MFE router a basename matching its prefix so its links resolve correctly within the larger app.
<BrowserRouter basename="/products">
...
</BrowserRouter>Cross-App Navigation
Sometimes one MFE must send the user into another (e.g. cart linking to checkout). The challenge: it should not import the other MFE router directly.
Navigating via the URL
The cleanest cross-app navigation just changes the URL. The shell router then mounts the right MFE — no direct coupling needed.
history.pushState({}, "", "/checkout");
window.dispatchEvent(new PopStateEvent("popstate"));A Shared Navigation Service
To avoid manual history hacks, the shell can expose a small navigate function that MFEs call, centralizing navigation logic.
import { navigate } from "shell/navigation";
navigate("/checkout");Keeping Routers in Sync
All MFEs must respond to the same browser history. Using one history source (the browser History API) keeps the shell and every MFE router synchronized on back/forward.
Avoiding Route Conflicts
Two MFEs claiming overlapping prefixes cause unpredictable rendering. Maintain a single registry of route prefixes to guarantee they never collide.
Lazy Loading per Route
Combine nested routing with lazy loading: only fetch an MFE bundle when the user navigates to its prefix, keeping initial load small.
const ProductApp = React.lazy(() => import("products/App"));Quick Check
Test your nested-routing knowledge.
Recap
You learned nested and cross-app navigation:
- Shell owns prefixes; MFEs own nested routes
- Use
basenameto scope MFE routers - Navigate across apps via URL or a shared service
- Share one browser history source
- Prevent prefix conflicts and lazy-load by route
Coordinated routing makes federated navigation feel seamless.
常见问题解答
「处理嵌套路由与跨应用导航」课时是免费的吗?
是的 — 「处理嵌套路由与跨应用导航」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Micro Frontends Architecture with Module Federation 课程的其余内容,请升级到 CoddyKit PRO。 Micro Frontends Architecture with Module Federation 课程共包含 4 节课。
「处理嵌套路由与跨应用导航」这节课中我会学到什么?
学习如何协调由各个微前端负责的嵌套路由,以及如何在不同 MFE 之间无缝导航。 你通过在浏览器中直接运行的动手代码来练习 Micro Frontends Architecture with Module Federation,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Micro Frontends Architecture with Module Federation 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Micro Frontends Architecture with Module Federation 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「处理嵌套路由与跨应用导航」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Micro Frontends Architecture with Module Federation 课中编写并运行代码吗?
能。每节 Micro Frontends Architecture with Module Federation 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 集中式路由策略
- 联邦化路由实现
- 深层链接与 URL 管理
- 处理嵌套路由与跨应用导航