Skip to content

Alert 提示

用于页面中展示重要的提示信息

基础用法

Alert 组件不属于浮层元素, 不会自动消失或关闭

Alert 组件提供四种类型, 由 type 指定, 默认为 info

<template>
  <div style="max-width: 600px">
    <px-alert title="Success alert" type="success" />
    <px-alert title="Info alert" type="info" class="mt-20" />
    <px-alert title="Warning alert" type="warning" class="mt-20" />
    <px-alert title="Error alert" type="danger" class="mt-20" />
  </div>
</template>

主题

Alert 组件提供了两个主题: lightdark

通过设置 effect 属性来改变主题, 默认为 light

<template>
  <div style="max-width: 600px">
    <px-alert title="Success alert" effect="dark" type="success" />
    <px-alert title="Info alert" effect="dark" type="info" class="mt-20" />
    <px-alert
      title="Warning alert"
      effect="dark"
      type="warning"
      class="mt-20"
    />
    <px-alert title="Error alert" effect="dark" type="danger" class="mt-20" />
  </div>
</template>

不可关闭

可以设置 Alert 组件是否为可关闭状态, closable 属性决定 Alert 组件是否可关闭, 该属性接受一个 Boolean, 默认为 false

<template>
  <px-alert title="Unclosable alert" type="success" :closable="false" />
  <px-alert title="Alert with callback" type="warning" @close="handleClose" />
</template>
<script lang="ts" setup>
const handleClose = () => {
  console.log('Alert closed.')
}
</script>

使用图标

通过设置 show-icon 属性来显示 Alert 组件的 icon, 这能更有效地向用户展示你的显示意图

<template>
  <div style="max-width: 600px">
    <px-alert title="Success alert" type="success" show-icon />
    <px-alert title="Info alert" type="info" show-icon />
    <px-alert title="Warning alert" type="warning" show-icon />
    <px-alert title="Error alert" type="danger" show-icon />
  </div>
</template>

文字居中

使用 center 属性使文字水平居中

<template>
  <div style="max-width: 600px">
    <px-alert title="Success alert" type="success" center show-icon />
    <px-alert title="Info alert" type="info" center show-icon />
    <px-alert title="Warning alert" type="warning" center show-icon />
    <px-alert title="Error alert" type="danger" center show-icon />
  </div>
</template>

文字描述

为 Alert 组件添加一个更加详细的描述来使用户了解更多信息

除了 required 的 title 属性外, 可以设置 description 属性来添加描述, 称之为辅助性文字, 只能用于存放文本内容

<template>
  <div style="max-width: 600px">
    <px-alert
      title="With description"
      type="success"
      description="This is a description."
    />
  </div>
</template>

带图标和描述

这里提供一个带有图标和描述的例子

<template>
  <div style="max-width: 600px">
    <px-alert
      title="Success alert"
      type="success"
      description="More text description"
      show-icon
    />
    <px-alert
      title="Info alert"
      type="info"
      description="More text description"
      show-icon
    />
    <px-alert
      title="Warning alert"
      type="warning"
      description="More text description"
      show-icon
    />
    <px-alert
      title="Error alert"
      type="danger"
      description="More text description"
      show-icon
    />
  </div>
</template>

Alert API

Props

NameDescriptionTypeDefault
titleAlert 标题string
typeAlert 类型enum - success | warning | danger | infoinfo
description描述性文本string
closable是否可以关闭booleantrue
center文字是否居中booleanfalse
show-icon是否展示图标booleanfalse
effect主题样式enum - light | darklight

Events

NameDescriptionType
close关闭 Alert 时触发的事件(event: MouseEvent) => void

Slots

NameDescription
default默认插槽,用于设置 Alert 的内容描述
title标题的内容

Expose

NameDescriptionType
open打开 Alert() => void
close关闭 Alert() => void
本站访客数 人次 本站总访问量