如何在 react-router 2.0.0-rc5 中获取当前路由
- 论坛
- 如何在 react-router 2.0.0-rc5 中获取当前路由
15 浏览
如何在 react-router 2.0.0-rc5 中获取当前路由
我有一个像下面这样的路由器:
我希望实现以下功能:
- 如果用户未登录,则将其重定向到
/login
- 如果用户已登录并试图访问
/login
,则将其重定向到根目录/
所以现在我正在尝试在App
的componentDidMount
方法中检查用户的状态,然后执行以下操作:
if (!user.isLoggedIn) { this.context.router.push('login') } else if(currentRoute == 'login') { this.context.router.push('/') }
问题在于我找不到获取当前路由的API。
我在这个已关闭的问题中找到了使用Router.ActiveState mixin和route handlers的建议,但是看起来这两个解决方案现在已被弃用。