防止 React Router Link 自动生成位置的路径
Prevent React Router Link from automatically generating the path of the location
也许我遗漏了什么,但我受困于 React Router Link 及其在除“/”之外的其他位置生成的路径。简单来说,当我在家('/')时,一切都很好,但是当我在位置(例如)http://somedomain.com/posts/newest 并使用 Link 组件时,如下所示:
<Link to="/title-for-the-article">Link to the article</Link>
Raw HTML a 渲染后的标签看起来像这样(这不是我想要的)
<a href="/posts/title-for-the-article">Link to the article</a>
如何防止路径“/posts/”附加到每个 Link 组件?我只想使用我通过 "to" 属性传递的确切路径。谢谢
仅使用 exact
参数 returns 匹配 url 的确切路径。
<Route exact path="/posts/title-for-the-article" component={Component} />
<Link to="/posts/title-for-the-article">Link to the article</Link>
// Location descriptor path to in string
<Link to={{ pathname: '/posts/title-for-the-article' }}/>
也许我遗漏了什么,但我受困于 React Router Link 及其在除“/”之外的其他位置生成的路径。简单来说,当我在家('/')时,一切都很好,但是当我在位置(例如)http://somedomain.com/posts/newest 并使用 Link 组件时,如下所示:
<Link to="/title-for-the-article">Link to the article</Link>
Raw HTML a 渲染后的标签看起来像这样(这不是我想要的)
<a href="/posts/title-for-the-article">Link to the article</a>
如何防止路径“/posts/”附加到每个 Link 组件?我只想使用我通过 "to" 属性传递的确切路径。谢谢
仅使用 exact
参数 returns 匹配 url 的确切路径。
<Route exact path="/posts/title-for-the-article" component={Component} />
<Link to="/posts/title-for-the-article">Link to the article</Link>
// Location descriptor path to in string
<Link to={{ pathname: '/posts/title-for-the-article' }}/>