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