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

Nic*_*eda 99 javascript url-routing reactjs react-router

我不明白使用IndexRouteIndexLink的目的是什么.似乎在任何情况下,除非关闭路径被激活,否则下面的代码将首先选择Home组件.

<Route path="/" component={App}>
  <IndexRoute component={Home}/>
  <Route path="about" component={About}/>
</Route>
Run Code Online (Sandbox Code Playgroud)

VS

<Route path="/" component={App}>
  <Route path="home" component={Home}/>
  <Route path="about" component={About}/>
</Route>
Run Code Online (Sandbox Code Playgroud)

第一种情况的优势/目的是什么?

Mic*_*ley 93

在上面例子中,将/会使AppHome作为一个孩子通过.在底部的例子,要/会使得App既不 Home也不About被渲染,因为无论其路径匹配.

对于旧版本的React Router,可以在关联版本的Index Routes和Index Links页面上获得更多信息.从4.0版开始,React Router不再使用IndexRoute抽象来实现相同的目标.

  • 那么我们在实践中可以使用这个功能呢? (3认同)