javascript - react 路由器 : What is the purpose of IndexRoute?

标签 javascript reactjs react-router url-routing

我不明白使用 IndexRouteIndexLink 的目的是什么。似乎无论如何下面的代码都会首先选择 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>

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

最佳答案

在上面的示例中,转到 / 会呈现 App,其中 Home 作为子级传递。在下面的示例中,转到 / 将呈现 App 既不 Home 也不 About 正在呈现,因为它们的路径都不匹配。

对于旧版本的 React Router,更多信息可在相关版本的 Index Routes and Index Links page 中找到。 .从 4.0 版开始,React Router 不再使用 IndexRoute 抽象来实现相同的目标。

关于javascript - react 路由器 : What is the purpose of IndexRoute?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32706913/

相关文章:

javascript - 自定义 Soundcloud 可视化工具 webgl

reactjs - 如何知道为我的 React 应用程序安装哪个版本的 Material UI?

reactjs - Material UI 与 React 16 的兼容性为 0.2 倍

javascript - URL随React Router更改但组件未渲染?

javascript - 在 JS/JQuery 中使用配置 PHP 文件

javascript - 将输入限制为 5 个字符(如果是整数)或 8 个字符(带分数)

javascript - react : Setinterval is not stopping even after clearing the timer using clearInterval

reactjs - 为什么 React Link 和 NavLink 不起作用?

reactjs - 如何将用户转发到 react 中的其他页面/路径?

javascript - 使用 switch case 的 Google 表单通知脚本