Alert
用法
标题 (Title)
使用 title 属性设置提示框的标题。
<template>
<UAlert title="Heads up!" />
</template>
描述 (Description)
使用 description 属性设置提示框的描述。
<template>
<UAlert title="Heads up!" description="You can change the primary color in your app config." />
</template>
图标 (Icon)
使用 icon 属性显示一个 Icon。
<template>
<UAlert title="Heads up!" description="您可以在应用配置中更改主色。" icon="i-lucide-terminal" />
</template>
头像 (Avatar)
使用 avatar 属性显示一个 Avatar。
<template>
<UAlert
title="Heads up!"
description="您可以在应用配置中更改主色。"
:avatar="{
src: 'https://github.com/nuxt.png'
}"
/>
</template>
颜色 (Color)
使用 color 属性更改提示框的颜色。
<template>
<UAlert
color="neutral"
title="Heads up!"
description="您可以在应用配置中更改主色(primary)。"
icon="i-lucide-terminal"
/>
</template>
变体 (Variant)
使用 variant 属性更改提示框的变体。
<template>
<UAlert
color="neutral"
variant="subtle"
title="Heads up!"
description="您可以在应用配置中更改主色(primary)。"
icon="i-lucide-terminal"
/>
</template>
关闭 (Close)
使用 close 属性显示一个 Button 来关闭提示框。
update:open 事件。<template>
<UAlert
title="Heads up!"
description="您可以在应用配置中更改主色(primary)。"
color="neutral"
variant="outline"
close
/>
</template>
您可以传递 Button 组件的任何属性来定制它。
<template>
<UAlert
title="Heads up!"
description="您可以在应用配置中更改主色。"
color="neutral"
variant="outline"
:close="{
color: 'primary',
variant: 'outline',
class: 'rounded-full'
}"
/>
</template>
关闭图标 (Close Icon)
使用 close-icon 属性自定义关闭按钮的 Icon。默认为 i-lucide-x。
<template>
<UAlert
title="Heads up!"
description="您可以在应用配置中更改主色。"
color="neutral"
variant="outline"
close
close-icon="i-lucide-arrow-right"
/>
</template>
操作 (Actions)
使用 actions 属性向提示框添加一些 Button 操作。
<template>
<UAlert
title="Heads up!"
description="您可以在应用配置中更改主色。"
color="neutral"
variant="outline"
:actions="[
{
label: 'Action 1'
},
{
label: 'Action 2',
color: 'neutral',
variant: 'subtle'
}
]"
/>
</template>
方向 (Orientation)
使用 orientation 属性更改提示框的方向。
<template>
<UAlert
title="Heads up!"
description="您可以在应用配置中更改主色。"
color="neutral"
variant="outline"
orientation="horizontal"
:actions="[
{
label: 'Action 1'
},
{
label: 'Action 2',
color: 'neutral',
variant: 'subtle'
}
]"
/>
</template>
示例
actions prop
actions 接受一个 按钮配置对象数组,每个对象支持 UButton 的所有属性:
<script setup lang="ts">
const handleAsyncAction = async () => {
// const data = await fetchData()
// showToast(`加载完成: ${data.length} 条记录`)
alert('点击动作按钮')
}
</script>
<template>
<UAlert
title="数据同步",
color="secondary"
:actions="[
{
label: '立即同步',
loadingIcon: 'i-heroicons-arrow-path',
onClick: handleAsyncAction, // 注意:在 v-bind 中,事件监听器需要使用 'onEventName' 的驼峰命名约定
loading: false // 可动态控制加载状态
}
]"
/>
</template>
class prop
使用 class prop 覆盖提示框的基本样式。
<template>
<UAlert title="Heads up!" description="您可以在应用配置中更改主色。" class="rounded-none" />
</template>
ui prop
使用 ui prop 覆盖提示框插槽的样式。
<template>
<UAlert
title="Heads up!"
description="您可以在应用配置中更改主色。"
icon="i-lucide-rocket"
:ui="{
icon: 'size-11'
}"
/>
</template>
API
Props
| Prop | Default | Type |
|---|---|---|
as |
|
The element or component this component should render as. |
title |
| |
description |
| |
icon |
| |
avatar |
| |
color |
|
|
variant |
|
|
orientation |
|
The orientation between the content and the actions. |
actions |
Display a list of actions:
| |
close |
|
Display a close button to dismiss the alert.
|
closeIcon |
|
The icon displayed in the close button. |
ui |
|
Slots
| Slot | Type |
|---|---|
leading |
|
title |
|
description |
|
actions |
|
close |
|
Emits
| Event | Type |
|---|---|
update:open |
|
使用示例
<script setup lang="ts">
import { ref } from 'vue'
const showAlert = ref(true)
</script>
<template>
<!-- 用 v-if 控制组件显示 -->
<UAlert
v-if="showAlert"
title="提示"
description="这是一个警告提示框"
color="warning"
variant="solid"
close
@update:open="showAlert = false"
/>
</template>
Theme
export default defineAppConfig({
ui: {
alert: {
slots: {
root: 'relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5',
wrapper: 'min-w-0 flex-1 flex flex-col',
title: 'text-sm font-medium',
description: 'text-sm opacity-90',
icon: 'shrink-0 size-5',
avatar: 'shrink-0',
avatarSize: '2xl',
actions: 'flex flex-wrap gap-1.5 shrink-0',
close: 'p-0'
},
variants: {
color: {
primary: '',
secondary: '',
success: '',
info: '',
warning: '',
error: '',
neutral: ''
},
variant: {
solid: '',
outline: '',
soft: '',
subtle: ''
},
orientation: {
horizontal: {
root: 'items-center',
actions: 'items-center'
},
vertical: {
root: 'items-start',
actions: 'items-start mt-2.5'
}
},
title: {
true: {
description: 'mt-1'
}
}
},
compoundVariants: [
{
color: 'primary',
variant: 'solid',
class: {
root: 'bg-primary text-inverted'
}
},
{
color: 'primary',
variant: 'outline',
class: {
root: 'text-primary ring ring-inset ring-primary/25'
}
},
{
color: 'primary',
variant: 'soft',
class: {
root: 'bg-primary/10 text-primary'
}
},
{
color: 'primary',
variant: 'subtle',
class: {
root: 'bg-primary/10 text-primary ring ring-inset ring-primary/25'
}
},
{
color: 'neutral',
variant: 'solid',
class: {
root: 'text-inverted bg-inverted'
}
},
{
color: 'neutral',
variant: 'outline',
class: {
root: 'text-highlighted bg-default ring ring-inset ring-default'
}
},
{
color: 'neutral',
variant: 'soft',
class: {
root: 'text-highlighted bg-elevated/50'
}
},
{
color: 'neutral',
variant: 'subtle',
class: {
root: 'text-highlighted bg-elevated/50 ring ring-inset ring-accented'
}
}
],
defaultVariants: {
color: 'primary',
variant: 'solid'
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
alert: {
slots: {
root: 'relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5',
wrapper: 'min-w-0 flex-1 flex flex-col',
title: 'text-sm font-medium',
description: 'text-sm opacity-90',
icon: 'shrink-0 size-5',
avatar: 'shrink-0',
avatarSize: '2xl',
actions: 'flex flex-wrap gap-1.5 shrink-0',
close: 'p-0'
},
variants: {
color: {
primary: '',
secondary: '',
success: '',
info: '',
warning: '',
error: '',
neutral: ''
},
variant: {
solid: '',
outline: '',
soft: '',
subtle: ''
},
orientation: {
horizontal: {
root: 'items-center',
actions: 'items-center'
},
vertical: {
root: 'items-start',
actions: 'items-start mt-2.5'
}
},
title: {
true: {
description: 'mt-1'
}
}
},
compoundVariants: [
{
color: 'primary',
variant: 'solid',
class: {
root: 'bg-primary text-inverted'
}
},
{
color: 'primary',
variant: 'outline',
class: {
root: 'text-primary ring ring-inset ring-primary/25'
}
},
{
color: 'primary',
variant: 'soft',
class: {
root: 'bg-primary/10 text-primary'
}
},
{
color: 'primary',
variant: 'subtle',
class: {
root: 'bg-primary/10 text-primary ring ring-inset ring-primary/25'
}
},
{
color: 'neutral',
variant: 'solid',
class: {
root: 'text-inverted bg-inverted'
}
},
{
color: 'neutral',
variant: 'outline',
class: {
root: 'text-highlighted bg-default ring ring-inset ring-default'
}
},
{
color: 'neutral',
variant: 'soft',
class: {
root: 'text-highlighted bg-elevated/50'
}
},
{
color: 'neutral',
variant: 'subtle',
class: {
root: 'text-highlighted bg-elevated/50 ring ring-inset ring-accented'
}
}
],
defaultVariants: {
color: 'primary',
variant: 'solid'
}
}
}
})
]
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import uiPro from '@nuxt/ui-pro/vite'
export default defineConfig({
plugins: [
vue(),
uiPro({
ui: {
alert: {
slots: {
root: 'relative overflow-hidden w-full rounded-lg p-4 flex gap-2.5',
wrapper: 'min-w-0 flex-1 flex flex-col',
title: 'text-sm font-medium',
description: 'text-sm opacity-90',
icon: 'shrink-0 size-5',
avatar: 'shrink-0',
avatarSize: '2xl',
actions: 'flex flex-wrap gap-1.5 shrink-0',
close: 'p-0'
},
variants: {
color: {
primary: '',
secondary: '',
success: '',
info: '',
warning: '',
error: '',
neutral: ''
},
variant: {
solid: '',
outline: '',
soft: '',
subtle: ''
},
orientation: {
horizontal: {
root: 'items-center',
actions: 'items-center'
},
vertical: {
root: 'items-start',
actions: 'items-start mt-2.5'
}
},
title: {
true: {
description: 'mt-1'
}
}
},
compoundVariants: [
{
color: 'primary',
variant: 'solid',
class: {
root: 'bg-primary text-inverted'
}
},
{
color: 'primary',
variant: 'outline',
class: {
root: 'text-primary ring ring-inset ring-primary/25'
}
},
{
color: 'primary',
variant: 'soft',
class: {
root: 'bg-primary/10 text-primary'
}
},
{
color: 'primary',
variant: 'subtle',
class: {
root: 'bg-primary/10 text-primary ring ring-inset ring-primary/25'
}
},
{
color: 'neutral',
variant: 'solid',
class: {
root: 'text-inverted bg-inverted'
}
},
{
color: 'neutral',
variant: 'outline',
class: {
root: 'text-highlighted bg-default ring ring-inset ring-default'
}
},
{
color: 'neutral',
variant: 'soft',
class: {
root: 'text-highlighted bg-elevated/50'
}
},
{
color: 'neutral',
variant: 'subtle',
class: {
root: 'text-highlighted bg-elevated/50 ring ring-inset ring-accented'
}
}
],
defaultVariants: {
color: 'primary',
variant: 'solid'
}
}
}
})
]
})
根据 Alert.vue 的 ui 计算结果进行配置
Alert.vue 的 ui 计算属性接收以下四个参数来计算变体:
color: props.colorvariant: props.variantorientation: props.orientationtitle: !!props.title || !!slots.title(这是一个布尔值)
这意味着你可以在 app.config.ts 中为 UAlert 组件配置这些 prop 对应的 variants。