React-Router: IndexRoute 的目的是什么?

我不明白使用 索引路由IndexLink的目的是什么。似乎在任何情况下,下面的代码都会首先选择 Home 组件,除非 About 路径被激活。

<Route path="/" component={App}>
<IndexRoute component={Home}/>
<Route path="about" component={About}/>
</Route>

<Route path="/" component={App}>
<Route path="home" component={Home}/>
<Route path="about" component={About}/>
</Route>

第一个例子的优点/目的是什么?

81069 次浏览

In the top example, going to / would render App with Home passed as a child. In the bottom example, going to / would render App with neither Home nor About being rendered, since neither of their paths match.

For older versions of React Router, more information is available on associated version's Index Routes and Index Links page. Starting in version 4.0, React Router no longer uses the IndexRoute abstraction to achieve the same goal.