<NuxtLink>

👉

Read more in Guide > Features > Routing.

Nuxt provides <NuxtLink> component to handle any kind of links within your application.

<NuxtLink> is a drop-in replacement for both Vue Router’s <RouterLink> component and HTML’s <a> tag. It intelligently determines whether the link is internal or external and renders it accordingly with available optimizations (prefetching, default attributes, etc.)

Examples

Basic usage

In this example, we use <NuxtLink> component to link to a website.

app.vue

  1. <template>
  2. <NuxtLink to="https://nuxtjs.org">
  3. Nuxt website
  4. </NuxtLink>
  5. <!-- <a href="https://nuxtjs.org" rel="noopener noreferrer">...</a> -->
  6. </template>

Open on StackBlitz <NuxtLink> - 图1

Internal routing

In this example, we use <NuxtLink> component to link to another page of the application.

pages/index.vue

  1. <template>
  2. <NuxtLink to="/about">
  3. About page
  4. </NuxtLink>
  5. <!-- <a href="/about">...</a> (+Vue Router & prefetching) -->
  6. </template>

Open on StackBlitz <NuxtLink> - 图2

target and rel attributes

In this example, we use <NuxtLink> with target, rel, and noRel props.

app.vue

  1. <template>
  2. <NuxtLink to="https://twitter.com/nuxt_js" target="_blank">
  3. Nuxt Twitter
  4. </NuxtLink>
  5. <!-- <a href="https://twitter.com/nuxt_js" target="_blank" rel="noopener noreferrer">...</a> -->
  6. <NuxtLink to="https://discord.nuxtjs.org" target="_blank" rel="noopener">
  7. Nuxt Discord
  8. </NuxtLink>
  9. <!-- <a href="https://discord.nuxtjs.org" target="_blank" rel="noopener">...</a> -->
  10. <NuxtLink to="https://github.com/nuxt" no-rel>
  11. Nuxt GitHub
  12. </NuxtLink>
  13. <!-- <a href="https://github.com/nuxt">...</a> -->
  14. <NuxtLink to="/contact" target="_blank">
  15. Contact page opens in another tab
  16. </NuxtLink>
  17. <!-- <a href="/contact" target="_blank" rel="noopener noreferrer">...</a> -->
  18. </template>

Open on StackBlitz <NuxtLink> - 图3

Props

  • to: Any URL or a route location object from Vue Router
  • href: An alias for to. If used with to, href will be ignored
  • target: A target attribute value to apply on the link
  • rel: A rel attribute value to apply on the link. Defaults to "noopener noreferrer" for external links.
  • noRel: If set to true, no rel attribute will be added to the link
  • activeClass: A class to apply on active links. Works the same as Vue Router’s active-class prop on internal links. Defaults to Vue Router’s default ("router-link-active")
  • exactActiveClass: A class to apply on exact active links. Works the same as Vue Router’s exact-active-class prop on internal links. Defaults to Vue Router’s default "router-link-exact-active")
  • replace: Works the same as Vue Router’s replace prop on internal links
  • ariaCurrentValue: An aria-current attribute value to apply on exact active links. Works the same as Vue Router’s aria-current-value prop on internal links
  • external: Forces the link to be considered as external (true) or internal (false). This is helpful to handle edge-cases

👉

Defaults can be overwritten, see overwriting defaults if you want to change them.

Overwriting defaults

You can overwrite <NuxtLink> defaults by creating your own link component using defineNuxtLink.

components/MyNuxtLink.js

  1. export default defineNuxtLink({
  2. name: 'MyNuxtLink',
  3. /* see signature below for more */
  4. })

You can then use <MyNuxtLink /> component as usual with your new defaults.

Open on StackBlitz <NuxtLink> - 图4

  1. defineNuxtLink({
  2. componentName?: string;
  3. externalRelAttribute?: string;
  4. activeClass?: string;
  5. exactActiveClass?: string;
  6. }) => Component
  • componentName: A name for the defined <NuxtLink> component.
  • externalRelAttribute: A default rel attribute value applied on external links. Defaults to "noopener noreferrer". Set it to "" to disable
  • activeClass: A default class to apply on active links. Works the same as Vue Router’s linkActiveClass option. Defaults to Vue Router’s default ("router-link-active")
  • exactActiveClass: A default class to apply on exact active links. Works the same as Vue Router’s linkExactActiveClass option. Defaults to Vue Router’s default ("router-link-exact-active")

🔎

Read and edit a live example in Examples > Routing > Nuxt Link