【问题标题】:Use absolute path or relative path in React Route在 React Route 中使用绝对路径或相对路径
【发布时间】:2016-11-09 02:17:18
【问题描述】:

全部:

当我在第 7 课学习它的官方教程时,我对 React Router 还是很陌生:

https://github.com/reactjs/react-router-tutorial/tree/master/lessons/07-more-nesting

当涉及到路由参数时:

// index.js
// ...
<Route path="/repos" component={Repos}>
  <Route path="/repos/:userName/:repoName" component={Repo}/>
</Route>

它开始使用绝对路径,我在想:

[1] React-Router 如何决定使用绝对路径或相对路径,仅仅是因为路径以斜杠“/”开头(我发现的一件事是:一旦我在相对路径中添加斜杠开始时,无论其父路由是什么,该路径都会变成绝对路径。)?

[2] 有没有办法可以使用相对参数路径?如果不是,那么路径为“/repos”的父Route的意义何在?

谢谢

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:
    1. React 路由器目前使用绝对路径,but they are working on relative routes

    2. 如果您想要相对路线,看起来人们正在从匹配参数获取当前路线,然后将路线附加到它。例如,&lt;Route path={match.path + '/more/stuff'}/&gt;

    在您给出的示例中,要点是,只要当前路径包含/repos,就会有一些内容(component={Repos} 生成的指向 repos 的链接列表)是可见的。当路径为 /repos/:userName/:repoName 时,它将继续显示该内容,因为路径仍然匹配 /repos,但它也会显示特定 repo 的内容 (component={Repo})。

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 1970-01-01
      • 2015-02-09
      • 2021-10-20
      • 2012-01-11
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      相关资源
      最近更新 更多