Link
用法
Link组件是<NuxtLink>的包装器,使用custom属性。它提供了一些额外的属性:
inactive-classprop在链接不活动时设置类,active-class在活动时使用。exact当链路处于活动状态并且路由与当前路由完全相同时,使用active-class进行样式设置。exact-query和exact-hash在链接处于活动状态并且查询或散列与当前查询或散列完全相同时使用active-class进行样式设置。- 使用
exact-query="partial"在链接处于活动状态并且查询与当前查询部分匹配时使用active-class进行样式设置。
- 使用
这背后的动机是在Nuxt 2 / Vue 2中提供与NuxtLink相同的API。您可以在Vue 2](https://router.vuejs.org/guide/migration/#removal-of-the-exact-prop-in-router-link)指南的Vue路由器[迁移中阅读更多信息。
Tag
当提供to属性时,Link组件会呈现<a>标记,否则会呈现<button>标记。您可以使用as属性更改回退标记。
<template>
<ULink as="button">Link</ULink>
</template>to属性来检查呈现的HTML。Style
默认情况下,链接有默认的活动和非活动样式,请查看#theme部分。
<template>
<ULink to="/docs/components/link">Link</ULink>
</template>to属性以查看活动和非活动状态。您可以使用raw属性覆盖此行为,并使用class、active-class和inactive-class提供您自己的样式。
<template>
<ULink raw to="/docs/components/link" active-class="font-bold" inactive-class="text-muted">Link</ULink>
</template>active-class和inactive-class道具的自动完成,您可以将以下设置添加到.vscode/settings.json:{
"tailwindCSS.classAttributes": [
"active-class",
"inactive-class"
]
}
区域设置:badge{label="4.7+" class="align-text-top"}
Link组件在安装时自动与@nuxtjs/i18n集成。内部链接使用$localePath帮助程序自动本地化,无需手动包装。
<template>
<!-- Automatically becomes /en/about or /fr/about based on current locale -->
<ULink to="/about">About</ULink>
</template>
localePath()或localeRoute()。API
Props
| 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" | stringA 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; } |
Theme
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'
]
}
]
}
}
})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'
]
}
]
}
}
})
]
})