react-router 4.0这个路由怎么配置呢?

图片描述

我实在不知道为什么所有url都会跳转到/latest,我想实现的是只是/跳转到/latest,其他url在刷新页面的时候不跳转,现在是所有url一旦刷新页面就跳转了。

阅读 8.4k
2 个回答

与之前版本不同,在 react-router4 中,渲染<redirect>组件会直接跳转到目标位置。from 属性仅用于<redirect>包裹着 <Switch> 组件中的时候。

Rendering a <Redirect> will navigate to a new location. The new location will override the current location in the history stack, like server-side redirects (HTTP 3xx) do

<Redirect to="/somewhere/else"/>  // render 该组件发生跳转

From: A pathname to redirect from. This can only be used to match a location when rendering a <Redirect> inside of a <Switch>. See <Switch children> for more details.

<Switch>
  <Redirect from='/old-path' to='/new-path'/>
  <Route path='/new-path' component={Place}/>
</Switch>

react-router4 文档 redirect

把redirect放到最后,路由匹配是从上到下的。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进