【问题标题】:React router redirect to a deeper url反应路由器重定向到更深的网址
【发布时间】:2017-09-08 07:48:38
【问题描述】:

我正在使用 react、redux、react-router v4、react-router-dom 和 react-router-redux。我有一个问题,我想在当前 URL 中附加一些东西,而不是替换当前 URL。代码是这样的:

const App = () => (
  <div>
    <main>
      <Switch>
        <Route path="/category/:categoryName" component={ProductList} />
        <Route path="/product/:productId/:infoType" component={ProductDetails} />
        <Redirect from='/product/:productId' to={`/product/:productId/details`}/>
        <Redirect from='/' to='/category/featured'/>
      </Switch>
    </main>
  </div>
)

对于这一行:

<Redirect from='/product/:productId' to={`/product/:productId/details`}/>

我想将详细信息附加到当前产品 url(例如 /product/1/details)。但相反,反应路由器正在重定向到 /product/:productId/details。

如何解决这个问题?有任何想法吗?提前致谢!!

【问题讨论】:

    标签: javascript reactjs react-router


    【解决方案1】:

    这个聚会很晚了,但是您将 to 属性作为字符串文字传递(但没有表达式)。相反,您应该使用

    to={`/product/${productID}/details`}
    

    当然,您需要将查询参数存储在productID 中。这将取决于您如何使用路由器 - 获取查询参数(如果可用)或分配您认为合适的 productID。

    【讨论】:

      猜你喜欢
      • 2019-03-30
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多