O componente Link é um wrapper em torno do <NuxtLink> usando a prop custom. Ele fornece algumas props extras:
inactive-class prop to set a class when the link is inactive, active-class is used when active.exact prop to style with active-class when the link is active and the route is exactly the same as the current route.exact-query and exact-hash props to style with active-class when the link is active and the query or hash is exactly the same as the current query or hash.
exact-query="partial" to style with active-class when the link is active and the query partially match the current query.A motivação por trás disso é oferecer a mesma API do NuxtLink dos tempos do Nuxt 2 / Vue 2. Você pode ler mais sobre isso no guia de migração do Vue 2 do Vue Router.
O componente Link renderiza uma tag <a> quando uma prop to é fornecida; caso contrário, renderiza uma tag <button>. Você pode usar a prop as para alterar a tag de fallback.
<template>
<NLink as="button">Link</NLink>
</template>
to.Por padrão, o link tem estilos de ativo e inativo padrão; confira a seção #theme.
<template>
<NLink to="/docs/components/link">Link</NLink>
</template>
to para ver os estados ativo e inativo.Você pode sobrescrever esse comportamento usando a prop raw e fornecer seus próprios estilos usando class, active-class e inactive-class.
<template>
<NLink raw to="/docs/components/link" active-class="font-bold" inactive-class="text-muted">Link</NLink>
</template>
active-class e inactive-class, você pode adicionar as seguintes configurações ao seu .vscode/settings.json:{
"tailwindCSS.classAttributes": [
"active-class",
"inactive-class"
]
}
O componente Link se integra automaticamente ao @nuxtjs/i18n quando instalado. Os links internos são localizados automaticamente usando o helper $localePath, sem exigir envolvimento manual.
<template>
<!-- Automatically becomes /en/about or /fr/about based on current locale -->
<NLink to="/about">About</NLink>
</template>
localePath() ou localeRoute() manualmente se necessário.| Prop | Default | Type |
|---|---|---|
as | 'button' | anyThe element or component this component should render as when not a link. |
type | 'button' | "reset" | "submit" | "button"The type of the button when not a link. |
disabled | boolean | |
active | undefined | boolean Force the link to be active independent of the current route. |
exact | boolean Will only be active if the current route is an exact match. | |
exactQuery | boolean | "partial" Allows controlling how the current route query sets the link as active. | |
exactHash | boolean Will only be active if the current route hash is an exact match. | |
inactiveClass | stringThe class to apply when the link is inactive. | |
raw | boolean When | |
locale | undefined | string | false | trueControl i18n auto-localization when
|
to | string | it | etRoute Location the link should navigate to when clicked on. | |
href | string | it | etAn alias for | |
external | boolean Forces the link to be considered as external (true) or internal (false). This is helpful to handle edge-cases | |
target | null | string & {} | "_blank" | "_parent" | "_self" | "_top"Where to display the linked URL, as the name for a browsing context. | |
rel | null | "noopener" | "noreferrer" | "nofollow" | "sponsored" | "ugc" | string & {}A rel attribute value to apply on the link. Defaults to "noopener noreferrer" for external links. | |
noRel | boolean If set to true, no rel attribute will be added to the link | |
prefetchedClass | stringA class to apply to links that have been prefetched. | |
prefetch | boolean When enabled will prefetch middleware, layouts and payloads of links in the viewport. | |
prefetchOn | "visibility" | "interaction" | Partial<{ visibility: boolean; interaction: boolean; }>Allows controlling when to prefetch links. By default, prefetch is triggered only on visibility. | |
noPrefetch | boolean Escape hatch to disable | |
trailingSlash | "remove" | "append"An option to either add or remove trailing slashes in the | |
activeClass | stringClass to apply when the link is active | |
exactActiveClass | stringClass to apply when the link is exact active | |
ariaCurrentValue | 'page' | "step" | "page" | "true" | "false" | "location" | "date" | "time"Value passed to the attribute |
viewTransition | boolean Pass the returned promise of | |
replace | boolean Calls | |
name | string | |
autofocus | false | true | "true" | "false" | |
form | string | |
formaction | string | |
formenctype | string | |
formmethod | string | |
formnovalidate | false | true | "true" | "false" | |
formtarget | string | |
referrerpolicy | "" | "no-referrer" | "no-referrer-when-downgrade" | "origin" | "origin-when-cross-origin" | "same-origin" | "strict-origin" | "strict-origin-when-cross-origin" | "unsafe-url" | |
download | any | |
hreflang | string | |
media | string | |
ping | string |
| Slot | Type |
|---|---|
default | { active: boolean; } |
export default defineAppConfig({
ui: {
link: {
base: 'focus-visible:outline-accent underline underline-offset-2',
variants: {
active: {
true: 'text-accent',
false: ''
},
disabled: {
true: 'cursor-not-allowed opacity-75'
}
},
compoundVariants: [
{
active: false,
disabled: false,
class: 'hover:text-accent transition-colors'
}
]
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nitro/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
link: {
base: 'focus-visible:outline-accent underline underline-offset-2',
variants: {
active: {
true: 'text-accent',
false: ''
},
disabled: {
true: 'cursor-not-allowed opacity-75'
}
},
compoundVariants: [
{
active: false,
disabled: false,
class: 'hover:text-accent transition-colors'
}
]
}
}
})
]
})