Link

GitHub
一个围绕的封装,带有额外属性。

用法

链接组件是<NuxtLink>的封装,使用自定义属性。它提供了几个额外属性

  • inactive-class 属性用于设置链接不活跃时的类名,active-class 用于活跃时。
  • exact 属性用于当链接活跃且路由与当前路由完全相同时,使用 active-class 样式。
  • exact-queryexact-hash 属性用于当链接活跃且查询或哈希与当前查询或哈希完全相同时,使用 active-class 样式。
    • 使用 exact-query="partial" 用于当链接活跃且查询部分匹配当前查询时,使用 active-class 样式。

这样做的目的是为了提供与 Nuxt 2 / Vue 2 中 NuxtLink 相同的 API。你可以在 Vue Router从 Vue 2 迁移指南中阅读更多内容。

它被 BreadcrumbButtonContextMenuDropdownMenuNavigationMenu 组件使用。

标签

当提供了 to 属性时,Link 组件会渲染一个 <a> 标签,否则它会渲染一个 <button> 标签。你可以使用 as 属性来更改回退标签。

<template>
  <ULink as="button">Link</ULink>
</template>
你可以通过更改 to 属性来检查渲染的 HTML。

样式

默认情况下,链接具有默认的活跃和不活跃样式,请查看#主题部分。

<template>
  <ULink to="/docs/components/link">Link</ULink>
</template>
尝试更改 to 属性以查看活跃和不活跃状态。

你可以通过使用 raw 属性并使用 classactive-classinactive-class 提供你自己的样式来覆盖此行为。

<template>
  <ULink raw to="/docs/components/link" active-class="font-bold" inactive-class="text-muted">Link</ULink>
</template>

智能感知

如果你正在使用 VSCode 并希望获得 active-classinactive-class 类名的自动补全,你可以将以下设置添加到你的 .vscode/settings.json 文件中

.vscode/settings.json
{
  "tailwindCSS.classAttributes": [
    "active-class",
    "inactive-class"
  ]
}

API

属性

属性默认值类型
as'button'any

此组件在不是链接时应呈现的元素或组件。

type'button'"reset" | "submit" | "button"

当不是链接时,按钮的类型。

disabledboolean
activeundefinedboolean

强制链接处于活动状态,独立于当前路由。

exactboolean

仅当当前路由完全匹配时才处于活跃状态。

exactQueryboolean | "partial"

允许控制当前路由查询如何将链接设置为活跃。

exactHashboolean

仅当当前路由哈希完全匹配时才处于活跃状态。

inactiveClassstring

链接不活跃时应用的类名。

rawboolean

true 时,仅应用来自 classactiveClassinactiveClass 的样式。

过渡到string | RouteLocationAsRelativeGeneric | RouteLocationAsPathGeneric

点击时链接应导航到的路由位置。

hrefstring | RouteLocationAsRelativeGeneric | RouteLocationAsPathGeneric

to 的别名。如果与 to 一起使用,href 将被忽略

externalboolean

强制将链接视为外部链接 (true) 或内部链接 (false)。这有助于处理边缘情况

targetnull | string & {} | "_blank" | "_parent" | "_self" | "_top"

在何处显示链接的 URL,作为浏览上下文的名称。

relnull | "noopener" | "noreferrer" | "nofollow" | "sponsored" | "ugc" | string & {}

应用于链接的 rel 属性值。外部链接默认为 "noopener noreferrer"。

noRelboolean

如果设置为 true,则链接不会添加 rel 属性

prefetchedClassstring

应用于已预取链接的类名。

prefetchboolean

启用后将预取视口中链接的中间件、布局和有效载荷。

prefetchOn"visibility" | "interaction" | Partial<{ visibility: boolean; interaction: boolean; }>

允许控制何时预取链接。默认情况下,仅在可见时触发预取。

noPrefetchboolean

禁用 prefetch 属性的紧急出口。

trailingSlash"append" | "remove"

用于为该特定链接添加或删除 href 中尾部斜杠的选项。如果提供,将覆盖全局 trailingSlash 选项。

activeClassstring

链接活跃时应用的类名

exactActiveClassstring

链接精确活跃时应用的类名

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

当链接精确活跃时,传递给 aria-current 属性的值。

viewTransitionboolean

如果支持,将 router.push() 返回的 Promise 传递给 document.startViewTransition()

replaceboolean

调用 router.replace 而不是 router.push

namestring
autofocusfalse | true | "true" | "false"
formstring
formactionstring
formenctypestring
formmethodstring
formnovalidatefalse | true | "true" | "false"
formtargetstring
referrerpolicy"" | "no-referrer" | "no-referrer-when-downgrade" | "origin" | "origin-when-cross-origin" | "same-origin" | "strict-origin" | "strict-origin-when-cross-origin" | "unsafe-url"
downloadany
hreflangstring
mediastring
pingstring
此组件还支持所有原生的 <a> HTML 属性。

插槽

插槽类型
default{ active: boolean; }

主题

app.config.ts
export default defineAppConfig({
  ui: {
    link: {
      base: 'focus-visible:outline-primary',
      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: 'focus-visible:outline-primary',
          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'
              ]
            }
          ]
        }
      }
    })
  ]
})

更新日志

184ea— chore: 减少类型冗余,通过省略操作按钮中的链接属性

da8da— 修复:定义 NuxtLinkProps 而不是从 #app 导入 (#5491)

5b177— feat: 扩展原生 HTML 属性 (#5348)

5cb65— 特性:导入 @nuxt/ui-pro 组件

9debc— 修复:将 active-class / inactive-class 与应用配置合并 (#4446)

67da9— 修复:Nuxt、Vue 和 Inertia 之间的行为一致 (#4134)

39c86— fix:@nuxt/module-builder 升级后重构类型(#3855)

c531d— 修复:处理 aria-current,如 NuxtLink / RouterLink