【发布时间】:2021-04-14 09:06:08
【问题描述】:
如何在 NextJS 的新选项卡中打开链接?我试过这个:
<Link href="https://twitter.com/" passHref>
<a target="_blank">
<div className={`${dark ? styles.iconTwitterWhite : styles.iconTwitter} mr-3`} />
</a>
</Link>
它会在新标签中打开链接,但我有一条 ESLint 消息说:
ESLint: The href attribute is required for an anchor to be keyboard accessible. Provide a valid, navigable address as the href value. If you cannot provide an href, but still need the element to resemble a link, use a button and change it with appropriate styles.
还有其他方法吗?
【问题讨论】:
-
这是在新标签页中打开的正确方法,但请确保将
rel="noopener noreferrer"添加到prevent the newly opened tab from being able to modify the original tab maliciously。关于 linting 问题,这是 Next.js<Link>组件的已知限制。在此处查看可能的解决方案:github.com/vercel/next.js/issues/5533
标签: next.js