如何将 React HashRouter 用于路由参数? [英] How to use React HashRouter for route parameters?

查看:37
本文介绍了如何将 React HashRouter 用于路由参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two routes, a base path and a customer route. The customer route takes a customerId parameter.

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

然后在反应组件中,我像这样调用它来导航

then in the react component I am calling it like this to navigate

      window.open(`/customer/${customerId}`)

(更新了,还是不行)

但是当我导航它时,它只会转到 App 组件.我必须使用 HashRouter,因为 React 应用程序包含在 Electron 中.BrowserRouter适用于网络

but when I navigate it just goes to the App component. I have to use HashRouter because the react app is wrapped in Electron. BrowserRouter works on the web

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

The url i get when i navigate is this: http://localhost:3000/customer/ca023754-bb75-4f64-a19c-958525b53e12#/ I also tried adding backslash in Route, /customer/:customerId/, that didn't work as well

我已阅读如何使用 React Router 和 Electron?但它并没有真正起作用

I have read How to use React Router with Electron? but it doesn't really work

推荐答案

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

I finally figured this out. Hashrouter expect the url to have "#/" after the basepath. so it should be window.open('#/customer/${customerId}')

这篇关于如何将 React HashRouter 用于路由参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆