React Router 的多个参数更改 url 但不是组件

沃瓦

我想点击链接按钮后,它会将我重定向到另一个带有完整 url 的页面,例如 -> localhost:3001/companies/name/forms

但它不会重定向到任何地方,每次单击时只更改 url .. like -> localhost:3001/companies/name/name/name/forms

<BrowserRouter>
...
<Switch>
  ...
  <Route path='/companies' exact component={CompanyList}></Route>
  <Route 
     path='/companies/:slug' 
     render={(props) => <CompanyDetails {...props} isAuthed={true} />}
   />
  <Route path='companies/:slug/forms' exact component={Forms}></Route>
<Switch>
...
</BrowserRouter>

并链接到 CompanyDetails 页面(组件)的路径

<Button type="button" className="btn btn-secondary">
      <Link to={`${this.props.match.params.slug}/forms`}>
         Create&nbsp; 
         <i className="fa fa-arrow-right"></i>
      </Link>
</Button>

有什么建议么?

尝试重新排列路线的顺序并添加 exact

同样在您的代码 path='companies/:slug/forms' 中缺少一个 /

<Route exact path='/companies/:slug/forms' exact component={Forms}></Route>      
<Route 
  exact
  path='/companies/:slug' 
  render={(props) => <CompanyDetails {...props} isAuthed={true} />}
/>

如果以上不起作用,请尝试/在 CompanyDetails 页面中添加到您的链接

<Link to={`/${this.props.match.params.slug}/forms`}>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章