简体   繁体   English

我如何使用reactjs中的路由从加载到另一个URL后从一个页面重定向

[英]How could i use the routing in reactjs to redirect from one page after loading to another url

Now I use routing like this: 现在我使用这样的路由:

<Route path='/main' component={App1}>
  <Route path='/main/:id' component={App2}>
    <Route path='/main/:id/basic' component={App3}/>
    <Route path='/main/:id/page1' component={App4}/>
  </Route>
</Route>

But how can i use something like this: 但我怎么能用这样的东西:

<Route path='/main' component={App1}>
  <Route path='/main/:id' component={App2, App3} />
  <Route path='/main/:id/page1' component={App4}/>
</Route>

or better this: 更好的这个:

<Route path='/main' component={App1}>
  <Route path='/main/:id' component={App2, App3}>
    <Route path='/main/:id/basic' component={App3}/>
    <Route path='/main/:id/page1' component={App4}/>
  </Route>
</Route>

Or redirect from /main/:id page to /main/:id/basic page after loading component App2 , because i should load components App2 (first) and App3 (second) by opening /main/:id url. 或者在加载组件App2后从/main/:id页面重定向到/main/:id/basic页面,因为我应该通过打开/main/:id url加载组件App2 (第一个)和App3 (第二个)。

It looks like you are using React Router 3. In this case use <IndexRedirect> component to achieve your goal: 看起来您正在使用React Router 3.在这种情况下,使用<IndexRedirect>组件来实现您的目标:

<Route path='/main' component={App1}>
  <Route path='/main/:id' component={App2}>
    <IndexRedirect to='basic' />
    <Route path='/main/:id/basic' component={App3}/>
    <Route path='/main/:id/page1' component={App4}/>
  </Route>
</Route>

Also, there is no need to use full paths in nested routes, for App3 and App4 , so your code could be a bit simplified: 此外,也没有必要使用嵌套的路线,全路径App3App4 ,所以你的代码可以有点简单:

<Route path='/main' component={App1}>
  <Route path=':id' component={App2}>
    <IndexRedirect to='basic' />
    <Route path='basic' component={App3}/>
    <Route path='page1' component={App4}/>
  </Route>
</Route>

More info about redirects can be found here 有关重定向的更多信息,请访问此处

By the way, check out version 4 of React Router . 顺便说一下,查看React Router的第4版 It was released recently and has bunch of nice features. 它最近发布,并有一堆很好的功能。

one route path only can handle one component. 一个路径路径只能处理一个组件。 you can still use current routes, so we will be redirect via component. 您仍然可以使用当前路由,因此我们将通过组件进行重定向。

<Route path='/main' component={App1}>
  <Route path='/main/:id' component={App2}>
    <Route path='/main/:id/basic' component={App3}/>
    <Route path='/main/:id/page1' component={App4}/>
  </Route>
</Route>

if you re using react-router v3 如果你使用react-router v3

in App2 -> componentDidMount , add for example App2 - > componentDidMount ,添加例如

this.context.router.push({ pathname: `/main/3/basic`})

or 要么

browserHistory.push('/main/3/basic')

detail about navigatoing outside component https://github.com/ReactTraining/react-router/blob/v3/docs/guides/NavigatingOutsideOfComponents.md 关于navigatoing外部组件的详细信息https://github.com/ReactTraining/react-router/blob/v3/docs/guides/NavigatingOutsideOfComponents.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM