阻止React Router Link自动生成位置路径

时间:2018-10-14 02:37:10

标签: reactjs react-router react-router-v4 react-router-dom

也许我遗漏了一些东西,但是我对React Router Link以及它在'/'以外的其他位置生成的路径感到困惑。简而言之,当我在家('/')时,一切都很好,但是当我在位置(例如)http://somedomain.com/posts/newest时,请使用这样的Link组件:

<Link to="/title-for-the-article">Link to the article</Link>

渲染后的

原始HTML a 标签(这不是我想要的)

<a href="/posts/title-for-the-article">Link to the article</a>

如何防止将路径“ / posts /”附加到每个Link组件?我只想使用通过“ to”属性传递的确切路径。谢谢。

1 个答案:

答案 0 :(得分:0)

使用exact参数只能返回与网址匹配的确切路径。

<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' }}/>