乐闻世界logo
搜索文章和话题

How to open a link in a new tab in nextjs

1个答案

1

To open a link in a new browser tab within Next.js, you typically set the target attribute of the <a> tag to _blank. This is a standard HTML feature and not exclusive to Next.js.

To implement this within the Link component of Next.js, you should wrap an <a> tag inside it and set the target="_blank" attribute on the <a> tag.

Here's an example:

jsx
import Link from 'next/link'; const MyComponent = () => { return ( <div> <h1>My Page</h1> <Link href="/about"> <a target="_blank" rel="noopener noreferrer">About Us</a> </Link> </div> ); }; export default MyComponent;

In this example, when the user clicks the 'About Us' link, it opens the /about page in a new tab.

Additionally, the rel="noopener noreferrer" attribute is used for security reasons. This prevents the new page from having access to the original page and protects users from potential malicious behavior via the window.opener API. This is strongly recommended, particularly when using target="_blank".

2024年6月29日 12:07 回复

你的答案