React-Router:IndexRoute 的目的是什么? [英] React-Router : What is the purpose of IndexRoute?

查看:17
本文介绍了React-Router:IndexRoute 的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I don't understand what the purpose of using an IndexRoute and IndexLink. It seems that in any case the code below would of selected the Home component first unless the About path was activated.

<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>

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

What's the advantage/purpose here of the first case?

推荐答案

在上面的例子中,转到 / 将使用 Home 渲染 App> 小时候就过去了.在底部示例中,转到 / 将呈现 Appnone HomeAbout> 正在渲染,因为它们的路径都不匹配.

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.

对于旧版本的 React Router,可以在相关版本的 索引路由和索引链接页面.从 4.0 版本开始,React Router 不再使用 IndexRoute 抽象来实现相同的目标.

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.

这篇关于React-Router:IndexRoute 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆