有没有办法在路由器中传递 id 而无需在 URL 中提及。
就像下面的路由一样,它的 URL 将有一个 id abc.com/abc/1
<Route exact path="/abc/:vid" component={AbcComponent} />
我希望 URL 为 abc.com/abc
和id
应该可以在 AbcComponent
中找到。我尝试了以下操作,但 App.js 中没有要传递的 id。
<Route exact path="/SpRides"
render={(props) => <SpRides vid={vid} spId={spId} depId={depId} {...props} />} />
最佳答案
如果你真的不希望 id 显示在 url 中(既不是查询字符串也不是哈希值),我认为唯一的解决方案可能是将其设置为位置状态: https://reactrouter.com/web/api/location
您可以设置将 Link“to” 属性作为对象传递的状态值: https://reactrouter.com/web/api/Link/to-object
关于reactjs - 将不带 url 的参数传递给 Router 中的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65529923/