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