<NuxtLink> 是 Vue Router 的 <RouterLink> 组件和 HTML 的 <a> 标签的直接替代品。它能智能地判断链接是*内部*还是*外部*链接,并根据可用优化(预加载、默认属性等)进行相应渲染。在此示例中,我们使用 <NuxtLink> 组件链接到应用程序的另一页。
<template>
<NuxtLink to="/about">About page</NuxtLink>
</template>
<!-- (Vue Router & Smart Prefetching) -->
<a href="/about">About page</a>
在此示例中,我们将 `id` 参数传递到路由 `~/pages/posts/[id].vue` 进行链接。
<template>
<NuxtLink :to="{ name: 'posts-id', params: { id: 123 } }">
Post 123
</NuxtLink>
</template>
<a href="/posts/123">Post 123</a>
默认情况下,<NuxtLink> 使用 Vue Router 的客户端导航进行相对路由。当链接到 `/public` 目录中的静态文件或同一域上托管的另一个应用程序时,可能会出现意外的 404 错误,因为它们不属于客户端路由。在这种情况下,您可以使用 `external` 属性与 <NuxtLink> 来绕过 Vue Router 的内部路由机制。
`external` 属性明确指示链接是外部链接。<NuxtLink> 将链接渲染为标准的 HTML <a> 标签。这确保了链接能够正确行为,绕过 Vue Router 的逻辑并直接指向资源。
对于 `/public` 目录中的静态文件,例如 PDF 或图像,请使用 `external` 属性以确保链接正确解析。
<template>
<NuxtLink
to="/example-report.pdf"
external
>
Download Report
</NuxtLink>
</template>
当指向同一域上的不同应用程序时,使用 `external` 属性可确保正确行为。
<template>
<NuxtLink
to="/another-app"
external
>
Go to Another App
</NuxtLink>
</template>
使用 `external` 属性或依赖自动处理可确保正确的导航,避免意外的路由问题,并提高与静态资源或跨应用场景的兼容性。
在此示例中,我们使用 <NuxtLink> 组件链接到一个网站。
<template>
<NuxtLink to="https://nuxtjs.org.cn">
Nuxt website
</NuxtLink>
<!-- <a href="https://nuxtjs.org.cn" rel="noopener noreferrer">...</a> -->
</template>
对于具有 `target` 属性的链接或绝对链接(例如,以 `http://`、`https://` 或 `//` 开头的链接),默认情况下会应用 `noopener noreferrer` 的 `rel` 属性。
这些默认设置对 SEO 没有负面影响,并被认为是最佳实践.
当您需要覆盖此行为时,可以使用 `rel` 或 `noRel` 属性。
<template>
<NuxtLink to="https://twitter.com/nuxt_js">
Nuxt Twitter
</NuxtLink>
<!-- <a href="https://twitter.com/nuxt_js" rel="noopener noreferrer">...</a> -->
<NuxtLink
to="https://discord.nuxtjs.org"
rel="noopener"
>
Nuxt Discord
</NuxtLink>
<!-- <a href="https://discord.nuxtjs.org" rel="noopener">...</a> -->
<NuxtLink
to="/about"
target="_blank"
>About page</NuxtLink>
<!-- <a href="/about" target="_blank" rel="noopener noreferrer">...</a> -->
</template>
可以使用 `noRel` 属性来防止将默认的 `rel` 属性添加到绝对链接。
<template>
<NuxtLink
to="https://github.com/nuxt"
no-rel
>
Nuxt GitHub
</NuxtLink>
<!-- <a href="https://github.com/nuxt">...</a> -->
</template>
Nuxt 自动包含智能预加载。这意味着它会检测链接何时可见(默认情况下),无论是在视口中还是在滚动时,并预加载这些页面的 JavaScript,以便在用户单击链接时准备就绪。Nuxt 仅在浏览器不忙时加载资源,并在连接离线或仅有 2g 连接时跳过预加载。
<NuxtLink to="/about" no-prefetch>
About page not pre-fetched
</NuxtLink>
<NuxtLink to="/about" :prefetch="false">
About page not pre-fetched
</NuxtLink>
在 `v3.13.0` 之后,我们现在支持 <NuxtLink> 的自定义预加载触发器。您可以使用 `prefetchOn` 属性来控制何时预加载链接。
<template>
<NuxtLink prefetch-on="visibility">
This will prefetch when it becomes visible (default)
</NuxtLink>
<NuxtLink prefetch-on="interaction">
This will prefetch when hovered or when it gains focus
</NuxtLink>
</template>
您还可以使用对象来配置 `prefetchOn`
<template>
<NuxtLink :prefetch-on="{ interaction: true }">
This will prefetch when hovered or when it gains focus
</NuxtLink>
</template>
您可能两者都不想启用!
<template>
<NuxtLink :prefetch-on="{ visibility: true, interaction: true }">
This will prefetch when hovered/focus - or when it becomes visible
</NuxtLink>
</template>
此配置将在元素进入视口时进行观察,并监听 `pointerenter` 和 `focus` 事件。由于这两种触发器都可能在不同条件下预加载同一资源,因此可能会导致不必要的资源使用或重复预加载。
要启用跨域预加载,您可以在 `nuxt.config` 中设置 `crossOriginPrefetch` 选项。这将使用Speculation Rules API.
export default defineNuxtConfig({
experimental: {
crossOriginPrefetch: true,
},
})
您也可以为您的应用程序全局启用/禁用所有链接的预加载。
export default defineNuxtConfig({
experimental: {
defaults: {
nuxtLink: {
prefetch: false,
},
},
},
})
当不使用 `external` 时,<NuxtLink> 支持 Vue Router 的所有`RouterLink` 属性
您可以在 `nuxt.config` 中覆盖一些 <NuxtLink> 默认值。
export default defineNuxtConfig({
experimental: {
defaults: {
nuxtLink: {
// default values
componentName: 'NuxtLink',
externalRelAttribute: 'noopener noreferrer',
activeClass: 'router-link-active',
exactActiveClass: 'router-link-exact-active',
prefetchedClass: undefined, // can be any valid string class name
trailingSlash: undefined, // can be 'append' or 'remove'
prefetch: true,
prefetchOn: { visibility: true },
},
},
},
})
您可以通过使用 `defineNuxtLink` 创建自己的链接组件来覆盖 <NuxtLink> 默认值。
export default defineNuxtLink({
componentName: 'MyNuxtLink',
/* see signature below for more */
})
然后,您可以像往常一样使用 `<MyNuxtLink />` 组件以及您的新默认设置。
interface NuxtLinkOptions {
componentName?: string
externalRelAttribute?: string
activeClass?: string
exactActiveClass?: string
trailingSlash?: 'append' | 'remove'
prefetch?: boolean
prefetchedClass?: string
prefetchOn?: Partial<{
visibility: boolean
interaction: boolean
}>
}
function defineNuxtLink (options: NuxtLinkOptions): Component {}