Link

GitHub
一个包装,附带额外的 prop。

用法

Link 组件是<NuxtLink>的包装,使用了自定义prop。它提供了一些额外的 prop:

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

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

它被 BreadcrumbButtonContextMenuDropdownMenuNavigationMenu 组件使用。

标签

Link 组件在提供了 to prop 时渲染一个 <a> 标签,否则渲染一个 <button> 标签。您可以使用 as prop 更改备用标签。

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

样式

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

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

您可以通过使用 raw prop 并使用 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

any

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

type

'button'

"button" | "reset" | "submit"

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

disabled

boolean

active

undefined

boolean

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

精确匹配

boolean

仅当当前路由完全匹配时才激活。

精确查询

boolean | "partial"

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

精确哈希

boolean

仅当当前路由哈希完全匹配时才激活。

不活跃类

string

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

原始

boolean

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

过渡到

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

href

字符串 | RouteLocationAsRelativeGeneric | RouteLocationAsPathGeneric

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

外部链接

boolean

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

target

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

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

rel

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

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

无 rel

boolean

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

预取类

string

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

预取

boolean

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

预取时机

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

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

不预取

boolean

禁用 prefetch 属性的应急方案。

活跃类

string

链接活跃时应用的类

精确活跃类

string

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

ariaCurrentValue

'page'

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

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

视图过渡

boolean

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

替换

boolean

调用 router.replace 而不是 router.push

插槽

插槽类型
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'
              ]
            }
          ]
        }
      }
    })
  ]
})

更新日志

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

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

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

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

c531d— 修复:像 NuxtLink / RouterLink 一样处理 aria-current