2017-08-22 34 views
1

在反應路由器(4.1.2),我有以下途徑呈現:獲得非精確的路線匹配路由PARAM

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

裏面我MapContainer我得到的organisationId通過this.props.match.params.organisationId

此預期MapContainer組件也變爲任何子路線一樣 /organisations/:organisationId/profiles/:profileId

可正常工作,但match道具不包含profileId,只是organisationId。哪種...有道理,因爲它是它找到的第一個「匹配」。但有什麼辦法可以在MapContainer中獲得:profileId,當它匹配一條非精確的路線時?

呈現兩種不同的路線與exact屬性似乎對我的使用情況下工作:

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

,但不知道那是做正確的方式。

對此的任何建議將是非常有益的。

回答

2

使用這個代替:

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

:profileId?意味着該參數是可選。所以它匹配這兩條路徑:

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