【发布时间】:2021-06-14 13:37:14
【问题描述】:
我正在开发一个带有自己的 react-router-dom 的 react 组件,以便能够处理像 /post 和 /post/123 这样的路径。
我需要将此组件打包为 npm 模块,以便它可以嵌入到另一个具有自己的 react-router-dom 的 React 应用程序中。
托管应用:
<Router>
<Switch>
<Route path="/posts" exact={true}>
<Blog />
</Route>
</Switch>
</Router
在博客组件中:
<Router basename="/posts">
<Switch>
<Route path="/">
<p>Display all posts</p>
</Route>
<Route path="/post/:id">
<p>Display details for post with id</p>
</Route>
</Switch>
</Router>
当我访问/posts 路径时,我收到Display all posts 消息。但是,当我访问 /post/123 路径时,我看不到 Display details for post with id 消息。
如何让内部路由器路径与外部路由器一起使用?
【问题讨论】:
-
如果您希望在其子项中匹配非精确路由,则托管应用程序中的
Route不能将exact属性设置为 true。 -
当我在托管应用程序中删除
exact属性并访问/posts/post/:id路由时,我得到了Display all posts文本。似乎问题是内部路由器无法检测到网址更改
标签: reactjs react-router