Skip to content

Alert 警告提示

警告提示,展现需要关注的信息。

代码演示

基础用法

最简单的用法,适用于简短的警告提示。

vue
<template>
  <a-alert message="Success Text" type="success" />
</template>

四种样式

共有四种样式 successinfowarningerror

vue
<template>
  <a-space direction="vertical" style="width: 100%">
    <a-alert message="Success Text" type="success" />
    <a-alert message="Info Text" type="info" />
    <a-alert message="Warning Text" type="warning" />
    <a-alert message="Error Text" type="error" />
  </a-space>
</template>

可关闭的警告提示

显示关闭按钮,点击可关闭警告提示。

vue
<template>
  <a-space direction="vertical" style="width: 100%">
    <a-alert
      message="Warning Text Warning Text Warning TextW arning Text Warning Text Warning TextWarning Text"
      type="warning"
      closable
      @close="onClose"
    />
    <a-alert
      message="Error Text"
      description="Error Description Error Description Error Description Error Description Error Description Error Description"
      type="error"
      closable
      @close="onClose"
    />
  </a-space>
</template>
<script setup>
const onClose = e => {
  console.log(e, 'I was closed.');
};
</script>

含有辅助性文字介绍

含有辅助性文字介绍的警告提示。

vue
<template>
  <a-space direction="vertical" style="width: 100%">
    <a-alert message="Success Text" type="success">
      <template #description>
        <p>
          Success Description
          <span style="color: red">Success</span>
          Description Success Description
        </p>
      </template>
    </a-alert>
    <a-alert
      message="Info Text"
      description="Info Description Info Description Info Description Info Description"
      type="info"
    />
    <a-alert
      message="Warning Text"
      description="Warning Description Warning Description Warning Description Warning Description"
      type="warning"
    />
    <a-alert
      message="Error Text"
      description="Error Description Error Description Error Description Error Description"
      type="error"
    />
  </a-space>
</template>

图标

可口的图标让信息类型更加醒目。

vue
<template>
  <a-space direction="vertical" style="width: 100%">
    <a-alert message="Success Tips" type="success" show-icon />
    <a-alert message="Informational Notes" type="info" show-icon />
    <a-alert message="Warning" type="warning" show-icon />
    <a-alert message="Error" type="error" show-icon />
    <a-alert
      message="Success Tips"
      description="Detailed description and advices about successful copywriting."
      type="success"
      show-icon
    />
    <a-alert
      message="Informational Notes"
      description="Additional description and informations about copywriting."
      type="info"
      show-icon
    />
    <a-alert
      message="Warning"
      description="This is a warning notice about copywriting."
      type="warning"
      show-icon
    />
    <a-alert
      message="Error"
      description="This is an error message about copywriting."
      type="error"
      show-icon
    />
  </a-space>
</template>

平滑地卸载

平滑、自然的卸载提示。

vue
<template>
  <a-alert
    v-if="visible"
    message="Alert Message Text"
    type="success"
    closable
    :after-close="handleClose"
  />
</template>
<script setup>
import { ref } from 'vue';
const visible = ref(true);
const handleClose = () => {
  visible.value = false;
};
</script>

操作

可以在右上角自定义操作项。

vue
<template>
  <a-space direction="vertical" style="width: 100%">
    <a-alert message="Success Tips" type="success" show-icon closable>
      <template #action>
        <a-button size="small" type="text">UNDO</a-button>
      </template>
    </a-alert>
    <a-alert
      message="Error Text"
      show-icon
      description="Error Description Error Description Error Description Error Description"
      type="error"
    >
      <template #action>
        <a-button size="small" danger>Detail</a-button>
      </template>
    </a-alert>
    <a-alert message="Warning Text" type="warning" closable>
      <template #action>
        <a-space>
          <a-button size="small" type="ghost">Done</a-button>
        </a-space>
      </template>
    </a-alert>
    <a-alert
      message="Info Text"
      description="Info Description Info Description Info Description Info Description"
      type="info"
      closable
    >
      <template #action>
        <a-space direction="vertical">
          <a-button size="small" type="primary">Accept</a-button>
          <a-button size="small" danger type="ghost">Decline</a-button>
        </a-space>
      </template>
    </a-alert>
  </a-space>
</template>

API

参数说明类型默认值
action自定义操作项slot-
afterClose关闭动画结束后触发的回调函数() => void-
banner是否用作顶部公告booleanfalse
closable默认不显示关闭按钮boolean
closeIcon自定义关闭 Iconslot<CloseOutlined />
closeText自定义关闭按钮string | slot
description警告提示的辅助性文字介绍string | slot
icon自定义图标,showIcon 为 true 时有效vnode | slot-
message警告提示内容string | slot
showIcon是否显示辅助图标boolean false,banner 模式下默认值为true
type指定警告提示的样式,有四种选择 success、info、warning、errorstringinfo,banner 模式下默认值为 warning

事件

事件名称说明回调参数
close关闭时触发的回调函数(e: MouseEvent) => void