简体   繁体   English

{Link}不能使用reactjs中正确的URL

[英]{Link} does not take to the correct URL in reactjs

I am learning Reactjs, and there is a problem when I try to use {Link} to go to external websites. 我正在学习Reactjs,尝试使用{Link}转到外部网站时出现问题。

<Link to={repo.html_url}>
{repo.name}
</Link>

Let's say the repo.html_url is 假设repo.html_url是
https://github.com/ningmeng7998/SmartER-Project-Android-Frontend . https://github.com/ningmeng7998/SmartER-Project-Android-Frontend
Instead of taking me to this URL, it takes me to http://localhost:3000/profile/https://github.com/ningmeng7998/SmartER-Project-Android-Frontend , which adds localhost in front of it. 无需将我带到此URL,而是带我到http:// localhost:3000 / profile / https://github.com/ningmeng7998/SmartER-Project-Android-Frontend ,它在其前面添加了localhost。

How to solve this problem? 如何解决这个问题呢?

I'm assuming you're using React Router's Link component, as it is not part of vanilla React. 我假设您正在使用React Router的Link组件,因为它不是普通React的一部分。

Currently, React Router Link s do not support external URLs, they're just for navigating internally in your app. 目前,React Router Link不支持外部URL,它们仅用于在应用程序内部进行导航。 You'll want to use a standard HTML anchor tag. 您将要使用标准的HTML锚标记。 Try this: 尝试这个:

<a href={repo.html_url}>{repo.name}</a>

There's some discussion going on around the issue of Link s supporting external URLs going on here if you'd like to learn more or stay in the loop. 这里也有一些讨论,打算在这问题Link小号支撑外部URL 会在这里 ,如果你想了解更多信息或留在循环。

Another solution is to implement your own wrapper around Link that handles the logic (and in the end spits out a <Link> component for internal routes or <a> tag for external URLs). 另一个解决方案是围绕处理逻辑的Link实现自己的包装器(最后为内部路由吐出<Link>组件,为外部URL吐出<a>标记)。

For external links, just use a tag! 对于外部链接,只需使用a标签! as <Link> is only used for internal routes. 因为<Link>仅用于内部路由。 As you can see in the docs that React Router themselves used a tags for an external link instead of Link . 正如您在文档中看到的那样,React Router本身使用a是外部链接a标签,而不是Link a标签。

<a href={repo.html_url}>
{repo.name}
</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM