簡體   English   中英

使用React Router的分層路由

[英]Hierarchical routes with react router

如果我想要一個路由層次結構,則將活動狀態應用於導航,但是某些級別的導航純粹是邏輯上的。

例如,如果我這樣做

<Route component={App} path="/" >
        <Route path="example" component={empty}>
            <Route path="ex1" component={ex1} />
            <Route path="ex2" component={ex2} />
            <IndexRedirect to="ex1" />
        </Route>
 </Route>

我的導航節點示例獲得了正確應用的活動狀態,但隨后我必須創建一個空組件,該組件僅呈現子級。

或者,我可以展平路線,這意味着我不再需要創建空白組件,而是不再獲得活動的導航狀態。

有一個好的解決方案嗎? (我正在使用最新的react-router v2.0.0rc5)

您可以省略空的組件。 我將您的示例寫為:

<Route path="/" component={App} >
  <Route path="example">
    <IndexRedirect to="ex1" />
    <Route path="ex1" component={ex1} />
    <Route path="ex2" component={ex2} />
  </Route>
</Route>

這將被認為是慣用的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM