我有一个如下所示的路由器:
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Index}/>
<Route path="login" component={Login}/>
</Route>
</Router>
这是我想要实现的目标:
/login
如果未登录,则将用户重定向到- 如果用户
/login
在已经登录的情况下尝试访问,请将其重定向到 root/
所以现在我正在尝试检查App
's 中的用户状态componentDidMount
,然后执行以下操作:
if (!user.isLoggedIn) {
this.context.router.push('login')
} else if(currentRoute == 'login') {
this.context.router.push('/')
}
这里的问题是我找不到获取当前路线的 API。
我发现这个关闭的问题建议使用 Router.ActiveState mixin 和路由处理程序,但看起来这两个解决方案现在已被弃用。