Alert 警告
用于页面中展示重要的提示信息。
基础用法
Alert 组件不属于浮层元素,不会自动消失或关闭。Alert 组件提供四种类型,由 type
属性指定,为 success | warning | danger | info
,默认值为 info
。
通过 slot 传入内容
Success alert
Info alert
Warning alert
Error alert
通过 prop 传入内容
Success alert
Info alert
Warning alert
Error alert
<template>
<h2>通过 slot 传入内容</h2>
<div style="max-width: 600px">
<n-alert type="success">Success alert</n-alert>
<n-alert type="info">Info alert</n-alert>
<n-alert type="warning">Warning alert</n-alert>
<n-alert type="danger">Error alert</n-alert>
</div>
<h2>通过 prop 传入内容</h2>
<div style="max-width: 600px">
<n-alert type="success" title="Success alert" />
<n-alert type="info" title="Info alert" />
<n-alert type="warning" title="Warning alert" />
<n-alert type="danger" title="Error alert" />
</div>
</template>
主题
Alert 组件提供了两个不同的主题:light
和 dark
。通过设置 effect
属性来改变主题,默认为 light
。
Success Alert
Info Alert
Warning Alert
Error Alert
<template>
<div style="max-width: 600px">
<n-alert title="Success Alert" type="success" effect="dark" />
<n-alert title="Info Alert" type="info" effect="dark" />
<n-alert title="Warning Alert" type="warning" effect="dark" />
<n-alert title="Error Alert" type="danger" effect="dark" />
</div>
</template>
可关闭的警告
可以设置 Alert 组件是否为可关闭状态,closable
属性决定 Alert 组件是否可关闭,该属性接受一个 Boolean
,默认为 false
。
Unclosable alert
Alert with callback
<script setup>
import { NMessage } from 'nano-ui-vue';
function handleClose() {
NMessage.info('close callback');
}
</script>
<template>
<div class="basic block">
<n-alert title="Unclosable alert" type="success" :closable="false" />
<n-alert title="Alert with callback" type="warning" @close="handleClose" />
</div>
</template>
带有图标
通过设置 show-icon
属性来显示 Alert 的图标,这能更有效地向用户展示你的显示意图。
Success alert
Info alert
Warning alert
Error alert
<template>
<div style="max-width: 600px">
<n-alert title="Success alert" type="success" show-icon />
<n-alert title="Info alert" type="info" show-icon />
<n-alert title="Warning alert" type="warning" show-icon />
<n-alert title="Error alert" type="danger" show-icon />
</div>
</template>
文字居中
使用 center
属性让文字水平居中。
Success alert
Info alert
Warning alert
Error alert
<template>
<div style="max-width: 600px">
<n-alert title="Success alert" type="success" center show-icon />
<n-alert title="Info alert" type="info" center show-icon />
<n-alert title="Warning alert" type="warning" center show-icon />
<n-alert title="Error alert" type="danger" center show-icon />
</div>
</template>
带有辅助性文字介绍
除了必填的 title
属性外,你可以设置 description
属性来帮助你更好地介绍,我们称之为辅助性文字。
With description
This is a description.
<template>
<div style="max-width: 600px">
<n-alert
title="With description"
type="success"
description="This is a description."
/>
</div>
</template>
带有图标和辅助性文字介绍
在具有辅助性文字介绍时,可以同时使用 show-icon
属性来展示图标。
Success alert
More text description
Info alert
More text description
Warning alert
More text description
Error alert
More text description
<template>
<div style="max-width: 600px">
<n-alert
title="Success alert"
type="success"
description="More text description"
show-icon
/>
<n-alert
title="Info alert"
type="info"
description="More text description"
show-icon
/>
<n-alert
title="Warning alert"
type="warning"
description="More text description"
show-icon
/>
<n-alert
title="Error alert"
type="danger"
description="More text description"
show-icon
/>
</div>
</template>
API
Props
名称 | 说明 | 类型 | 默认值 |
---|---|---|---|
title | 标题 | string | - |
type | 类型 | 'success' | 'warning' | 'info' | 'danger' | 'info' |
description | 辅助性文字 | string | - |
closable | 是否可关闭 | boolean | false |
center | 文字是否居中 | boolean | false |
show-icon | 是否显示图标 | boolean | false |
effect | 主题 | 'light' | 'dark' | 'light' |
Events
事件名 | 说明 | 回调参数 |
---|---|---|
close | 关闭 Alert 时触发的事件 | - |
Slots
插槽名 | 说明 |
---|---|
default | Alert 内容 |
title | 标题的内容 |
样式变量
变量名 | 说明 | 默认值 |
---|---|---|
--nano-alert-padding | 内边距 | 12px 16px |
--nano-alert-margin | 外边距 | 20px 0 0 |
--nano-alert-font-size | 字体大小 | var(--nano-font-size-base) |
--nano-alert-line-height | 行高 | 1.4 |
--nano-alert-border-radius | 圆角 | var(--nano-border-radius-base) |
--nano-alert-icon-size | 图标大小 | 16px |
--nano-alert-icon-margin | 图标外边距 | 8px |
--nano-alert-title-font-size | 标题字体大小 | var(--nano-font-size-base) |
--nano-alert-title-line-height | 标题行高 | 24px |
--nano-alert-title-font-weight | 标题字重 | 700 |
--nano-alert-desc-font-size | 描述字体大小 | var(--nano-font-size-base) |
--nano-alert-desc-line-height | 描述行高 | 21px |
--nano-alert-desc-margin | 描述外边距 | 5px 0 0 |
--nano-alert-close-size | 关闭按钮大小 | 16px |