如何从路由器组件以外的组件访问路由参数 [英] How to access route params from a component other then the Router component
问题描述
在 React-router 1.0/2.0 中,可以从 Route 组件中以 this.props.params
的形式访问路由参数 - 但似乎没有支持的方法来从上面包含的组件中访问参数路由组件.
In React-router 1.0/2.0 one can access routing params as this.props.params
from a Route component - but there seems no supported way to access params from a component deeply contained in the above Route component.
可以通过 context.location
访问数据类型,但这会引发警告,说明已弃用.
The data sort of could be accessed through context.location
but this one throws a warning, stating this is deprecated.
推荐答案
在最新版本的 React Router 中,所有上下文数据都已被弃用,取而代之的是 this.context.router
:
Starting in the latest version of React Router, all the context data has been deprecated in favor of this.context.router
:
仅将名为 router
的对象添加到上下文中.访问 this.context.history
、this.context.location
和 this.context.route
都已弃用.这个新对象包含 history
中可用的方法(例如 push
、replace
)以及 setRouteLeaveHook
.>
Only an object named
router
is added to context. Accessingthis.context.history
,this.context.location
, andthis.context.route
are all deprecated. This new object contains the methods available fromhistory
(such aspush
,replace
) along withsetRouteLeaveHook
.
不幸的是,这不包括 location
数据,因此您需要自己将其传递给子(非路由处理程序)组件.
Unfortunately, this doesn't include the location
data, so you'll need to pass it yourself into child (non-route handler) components.
这篇关于如何从路由器组件以外的组件访问路由参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!