【问题标题】:Route to same React Component even if no path parameter is provided即使没有提供路径参数,也路由到相同的 React 组件
【发布时间】:2020-11-09 11:04:51
【问题描述】:

即使没有提供路径参数,我也会尝试在我的应用程序中呈现相同的组件:

  <Switch>
    <Route
      path="/productInfoList/:product"
      component={ProductInfoList}
     />
  </Switch>

但是,如果 URL 中没有提供 ":product" 参数,则不会呈现组件。有没有办法渲染相同的“ProductInfoList”组件,即使没有提供路径参数?

我觉得除了为path="/productInfoList" 重新声明一条新路线之外,必须有一种有效的方法。

【问题讨论】:

  • 听起来你的组件可以在没有 /:product pram 的情况下工作,所以如果路径 pram 已通过,则将其从路径中删除并在组件中进行检查,如果是,则执行逻辑

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


【解决方案1】:

您好,您可以选择没有婴儿车的路线 然后只需传入 history.push 或链接

history.push({
  pathname: '/template',
  search: '?query=abc',
  state: { detail: response.data }
})
<Link to={{
      pathname: '/template',
      search: '?query=abc',
      state: { detail: response.data }
    }}> My Link </Link>

如您在此处看到的,如果您想获取可以使用的数据,您可以传递一个状态 props.location.state.detail 像这样,您可以跟踪您是否获得状态,然后按照您的意愿使用它

【讨论】:

    猜你喜欢
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2022-01-21
    • 2020-03-11
    相关资源
    最近更新 更多