【问题标题】:How to use React HashRouter for route parameters?如何使用 React HashRouter 作为路由参数?
【发布时间】:2020-04-06 08:08:29
【问题描述】:

我有两条路线,一条基本路线和一条客户路线。客户路线采用 customerId 参数。

  <HashRouter>
    <Switch>
      <Route path="/mapper/:mappingId/" exact component={Mapper}/>
      <Route path="/" exact component={App} />
    </Switch>
  </HashRouter>

然后在 react 组件中我这样称呼它来导航

      window.open(`/customer/${customerId}`)
(更新了,还是不行)

但是当我导航时它只是转到 App 组件。我必须使用 HashRouter,因为 react 应用程序包含在 Electron 中。 BrowserRouter 在网络上工作

我导航时得到的网址是这样的: http://localhost:3000/customer/ca023754-bb75-4f64-a19c-958525b53e12#/ 我还尝试在 Route 中添加反斜杠 /customer/:customerId/,但效果不佳

我已经阅读了How to use React Router with Electron?,但它并没有真正起作用

【问题讨论】:

  • 最好将“/”放在最前面,并带有确切的路径,并将您的路线放在开关内。
  • 我更新了代码 sn-p 并尝试在路径变量之前和之后放置“精确”。还是不行
  • 把默认路由'/'放在第一位。

标签: reactjs react-router


【解决方案1】:

我终于想通了。 Hashrouter 期望 url 在基本路径之后有“#/”。所以应该是 window.open('#/customer/${customerId}')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-16
    • 2016-09-09
    • 2018-06-18
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多