获取非精确路由匹配的路由参数

Get route param on non-exact route match

在 React 路由器 (4.1.2) 中,我呈现了以下路由:

<Route path="/organisations/:organisationId/profiles" component={MapContainer} />

在我的 MapContainer 中,我通过 this.props.match.params.organisationId

获得了预期的 organizationId

MapContainer 组件也呈现在任何子路由上,例如 /organisations/:organisationId/profiles/:profileId

这按预期工作,但 match 属性不包含 profileId,仅包含 organisationId。哪个...有点道理,因为它是它为路线找到的第一个 "match"。但是有没有办法在匹配到非精确路由时获取MapContainer中的:profileId?

使用 exact 属性 呈现两条不同的路线似乎适用于我的用例:

<Route exact path="/organisations/:organisationId/profiles" component={MapContainer} />
<Route exact path="/organisations/:organisationId/profiles/:profileId" component={MapContainer} />

但不确定这是正确的方法。

对此的任何建议都会非常有帮助。

改用这个:

<Route path="/organisations/:organisationId/profiles/:profileId?" component={MapContainer} />

:profileId?表示该参数是可选的。所以它匹配这两条路径:

/organisations/apple/profiles
/organisations/apple/profiles/1