【问题标题】:How do I navigate from a React Child route back to a parent如何从 React Child 路由导航回父级
【发布时间】:2020-05-23 20:23:18
【问题描述】:

在我的应用程序中,我有一个博客模块。当路由匹配“/blog”时,Blog Root 组件将根据以下内容呈现:

<Route path="/blog" component={BlogRoot} />

在这个组件中有一个子路由器:

<BrowserRouter>
  <Switch>
    <Route path={`${match.path}/:slug`} component={BlogPost} />
    <Route path={match.path} exact component={BlogListing} />
  </Switch>
</BrowserRouter>

我无法从 BlogPost 组件(匹配 ${match.path}/:slug)导航回列表(匹配 path={match.path} exact)。在 Blog Post 路由上时,使用 Link to="/blog" 不会呈现 Blog Listing 组件(它停留在当前的 BlogPost 上)。

我做错了什么?

提前致谢。

【问题讨论】:

  • 首先你必须在项目中使用单个 BrowserRouter 第二个时刻改变你在 Switch 中路由的顺序。首先是path={match.path} exact,然后是path={${match.path}/:slug}

标签: reactjs react-router react-router-dom react-router-v4


【解决方案1】:

之所以停留在同一个组件,是因为找不到下一条路线。 为此,您需要先定义动态路由 (/:slug),然后再使用组件路由。

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-06
    • 2017-12-23
    • 2020-02-19
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多