关于reactjs:使用备用路由配置对路由器进行重定向

React Router redirect using alternate route configuration

我想使用React Router的备用配置:https://github.com/rackt/react-router/blob/1.0.x/docs/guides/basics/RouteConfiguration.md#alternate-configuration

但是,我无法重定向到上班。我试过了:

1
2
3
4
5
6
7
8
9
10
11
const routes = {
  childRoutes: [
    { path: '/test', component: Test },
  ],
  component: App,
  path: '/',
  redirect: {
    from: '/',
    to: '/test',
  },
};

据我所知,没有任何文档。功能是否存在?应该怎么做?


将发布的链接中的示例代码与上面的示例(在保留URL中)进行比较,我认为这两个示例均指的是设置相同的路由(唯一的不同可能是最后一个使用备用配置)。我们可以推断出当前进行重定向的方式是使用onEnter函数。

例如,如果需要(使用JSX):

1
<Redirect from="messages/:id" to="/messages/:id" />

并且您使用备用配置,则必须这样编写:

1
2
3
4
5
{ path: 'messages/:id',
  onEnter: function (nextState, replaceState) {
    replaceState(null, '/messages/' + nextState.params.id)
  }
}

更新:您的特殊情况很特殊,因为您要从/重定向,因此您可能想尝试使用IndexRedirect或indexroute。