Link

GitHub
Een wikkel rond NuxtLink met extra rekwisieten.

Gebruik

De Link-component is een wikkel om <NuxtLink> met behulp van de custom prop. Het biedt een paar extra rekwisieten:

  • inactive-class prop om een klasse in te stellen wanneer de link inactief is, active-class wordt gebruikt wanneer deze actief is.
  • exact prop om te stylen met active-class wanneer de link actief is en de route exact hetzelfde is als de huidige route.
  • exact-query en exact-hash rekwisieten om te stylen met active-class wanneer de link actief is en de query of hash exact hetzelfde is als de huidige query of hash.
  • use exact-query="partial" om te stylen met active-class wanneer de link actief is en de query gedeeltelijk overeenkomt met de huidige query.

De stimulans hierachter is om dezelfde API als NuxtLink terug te bieden in Nuxt 2 / Vue 2. Je kunt er meer over lezen in de Vue Router migratie van Vue 2 gids.

Het wordt gebruikt door de Breadcrumb, Button, ContextMenu, DropdownMenu en NavigationMenu componenten.

Tag

De Link-componenten geven een <a>-tag weer wanneer een to-prop wordt geleverd, anders wordt er een <button>-tag weergegeven. U kunt de as-prop gebruiken om de fallback-tag te wijzigen.

<template>
  <ULink as="button">Link</ULink>
</template>
U kunt de gerenderde HTML inspecteren door de to prop te wijzigen.

Stijl

Standaard heeft de link standaard actieve en inactieve stijlen, bekijk de #theme sectie.

<template>
  <ULink to="/docs/components/link">Link</ULink>
</template>
Probeer de to-prop te wijzigen om de actieve en inactieve toestanden te zien.

U kunt dit gedrag overschrijven door de raw prop te gebruiken en uw eigen stijlen aan te bieden met class, active-class en inactive-class.

<template>
  <ULink raw to="/docs/components/link" active-class="font-bold" inactive-class="text-muted">Link</ULink>
</template>
Als u de Tailwind CSS IntelliSense extensie voor VSCode gebruikt en u wilt automatisch aanvullen voor de active-class- en inactive-class-rekwisieten, kunt u de volgende instellingen aan uw .vscode/settings.json toevoegen:
.vscode/settings.json
{
  "tailwindCSS.classAttributes": [
    "active-class",
    "inactive-class"
  ]
}

Lokaal: badge{label="4.7+" class="align-text-top"}

De Link-component integreert automatisch met @nuxtjs/i18n wanneer geïnstalleerd. Interne links worden automatisch gelokaliseerd met behulp van de $localePath-helper zonder handmatige verpakking.

<template>
  <!-- Automatically becomes /en/about or /fr/about based on current locale -->
  <ULink to="/about">About</ULink>
</template>
U kunt desgewenst nog steeds handmatig localePath() of localeRoute() gebruiken.
Meer informatie over Internationalization in Nuxt UI.

API

Props

Prop Default Type
as
'button'
any

The 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
string

The class to apply when the link is inactive.

raw
boolean

When true, only styles from class, activeClass, and inactiveClass will be applied.

locale
undefined
string | false | true

Control i18n auto-localization when @nuxtjs/i18n is installed.

  • undefined / true (default): auto-localizes to the current locale using $localePath. Paths already carrying a locale prefix (from e.g. switchLocalePath()) are detected and left untouched to prevent double-prefixing.
  • false: explicitly disables auto-localization.
  • string: localizes to a specific locale (e.g. 'fr').
to
string | it | et

Route Location the link should navigate to when clicked on.

href
string | it | et

An alias for to. If used with to, href will be ignored

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
string

A 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 prefetch attribute.

trailingSlash
"remove" | "append"

An option to either add or remove trailing slashes in the href for this specific link. Overrides the global trailingSlash option if provided.

activeClass
string

Class to apply when the link is active

exactActiveClass
string

Class to apply when the link is exact active

ariaCurrentValue
'page'
"step" | "page" | "true" | "false" | "location" | "date" | "time"

Value passed to the attribute aria-current when the link is exact active.

viewTransition
boolean

Pass the returned promise of router.push() to document.startViewTransition() if supported.

replace
boolean

Calls router.replace instead of router.push.

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
Dit onderdeel ondersteunt ook alle native <a> HTML-kenmerken.

Slots

Slot Type
default
{ active: boolean; }

Thema

app.config.ts
export default defineAppConfig({
  ui: {
    link: {
      base: 'outline-primary/25 focus-visible:outline-3 rounded-md',
      variants: {
        active: {
          true: 'text-primary',
          false: 'text-muted'
        },
        disabled: {
          true: 'cursor-not-allowed opacity-75'
        }
      },
      compoundVariants: [
        {
          active: false,
          disabled: false,
          class: [
            'hover:text-default',
            'transition-colors'
          ]
        }
      ]
    }
  }
})
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'

export default defineConfig({
  plugins: [
    vue(),
    ui({
      ui: {
        link: {
          base: 'outline-primary/25 focus-visible:outline-3 rounded-md',
          variants: {
            active: {
              true: 'text-primary',
              false: 'text-muted'
            },
            disabled: {
              true: 'cursor-not-allowed opacity-75'
            }
          },
          compoundVariants: [
            {
              active: false,
              disabled: false,
              class: [
                'hover:text-default',
                'transition-colors'
              ]
            }
          ]
        }
      }
    })
  ]
})

Wijzigingsgelog

No recent changes