我的路线如下所示:
<Route path="sites/:siteid" component={SiteContainer} >
<Route path="restore/:version/:action" component={RestoreContainer} />
<Route path="migrate/:version/:action" component={MigrateContainer} />
</Route>
如果我当前的网址是:
sites/1/restore/0/list
在SiteContainer
React组件内部,我如何知道childRoutes
当前正在提供哪个服务?如本例所示childRoutes[0]
。
我终于想通了。Reactchildren
通过以下方式为您提供了访问道具的权限:
this.props.children
现在,当我访问URL时:
sites/1
正在访问主要路线,即loading SiteContainer
。因此,没有孩子null
。
在网址上时:
sites/1/restore/0/list
第一个子路由的组件(即RestoreContainer)变为children
。现在,我可以使用以下方式访问路线:
this.props.children.props.route
为了处理这两种情况,我在以下内容中进行了说明SiteContainer
:
childroute = this.props.children && this.props.children.props.route
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句