[英]How to get current route in react-router 2.0.0-rc5
I have a router like below:我有一个如下所示的路由器:
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Index}/>
<Route path="login" component={Login}/>
</Route>
</Router>
Here's what I want to achieve :这是我想要实现的目标:
/login
if not logged in如果未登录,则将用户重定向到/login
/login
when they are already logged in, redirect them to root /
如果用户在已经/login
时尝试访问/login
,请将其重定向到 root /
so now I'm trying to check user's state in App
's componentDidMount
, then do something like:所以现在我试图在App
的componentDidMount
检查用户的状态,然后执行以下操作:
if (!user.isLoggedIn) {
this.context.router.push('login')
} else if(currentRoute == 'login') {
this.context.router.push('/')
}
The problem here is I can't find the API to get current route.这里的问题是我找不到获取当前路线的 API。
I found this closed issue suggested using Router.ActiveState mixin and route handlers, but it looks like these two solutions are now deprecated.我发现这个关闭的问题建议使用 Router.ActiveState 混合和路由处理程序,但看起来这两个解决方案现在已被弃用。
After reading some more document, I found the solution:阅读更多文档后,我找到了解决方案:
https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/location.md https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/location.md
I just need to access the injected property location
of the instance of the component like:我只需要访问组件实例的注入属性location
,例如:
var currentLocation = this.props.location.pathname
You can get the current route using您可以使用获取当前路线
const currentRoute = this.props.routes[this.props.routes.length - 1];
...which gives you access to the props from the lowest-level active <Route ...>
component. ...这使您可以从最低级别的活动<Route ...>
组件访问道具。
Given...鉴于...
<Route path="childpath" component={ChildComponent} />
currentRoute.path
returns 'childpath'
and currentRoute.component
returns function _class() { ... }
. currentRoute.path
返回'childpath'
并且currentRoute.component
返回function _class() { ... }
。
If you use the history,then Router put everything into the location from the history,such as:如果使用历史记录,则路由器将所有内容放入历史记录中的位置,例如:
this.props.location.pathname;
this.props.location.query;
get it?得到它?
As of version 3.0.0, you can get the current route by calling:从 3.0.0 版本开始,您可以通过调用获取当前路由:
this.context.router.location.pathname this.context.router.location.pathname
Sample code is below:示例代码如下:
var NavLink = React.createClass({
contextTypes: {
router: React.PropTypes.object
},
render() {
return (
<Link {...this.props}></Link>
);
}
});
For any users having the same issue in 2017, I solved it the following way:对于在 2017 年遇到相同问题的任何用户,我通过以下方式解决了它:
NavBar.contextTypes = {
router: React.PropTypes.object,
location: React.PropTypes.object
}
and use it like this:并像这样使用它:
componentDidMount () {
console.log(this.context.location.pathname);
}
在App.js
添加以下代码并尝试
window.location.pathname
You could use the 'isActive' prop like so:您可以像这样使用“isActive”道具:
const { router } = this.context;
if (router.isActive('/login')) {
router.push('/');
}
isActive will return a true or false. isActive 将返回真或假。
Tested with react-router 2.7使用 react-router 2.7 测试
Works for me in the same way:以同样的方式为我工作:
...
<MyComponent {...this.props}>
<Route path="path1" name="pname1" component="FirstPath">
...
</MyComponent>
...
And then, I can access "this.props.location.pathname" in the MyComponent function.然后,我可以在 MyComponent 函数中访问“this.props.location.pathname”。
I forgot that it was I am...))) Following link describes more for make navigation bar etc.: react router this.props.location我忘记了我是...))) 以下链接描述了制作导航栏等的更多信息: react router this.props.location
Try grabbing the path using: document.location.pathname
尝试使用以下方法获取路径: document.location.pathname
In Javascript you can the current URL in parts.在 Javascript 中,您可以分部分获取当前 URL。 Check out: https://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/查看: https : //css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.