Skip to content

Alert 提示

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

ts
import { Alert as ElAlert } from '@szhn/dh-design-pc'

基础用法

Alert 组件不属于浮层元素,不会自动消失或关闭。提供 5 种类型,由 type 属性指定,默认值为 info

<template>
  <div class="demo-alert">
    <el-alert title="成功提示的文案" type="success" />
    <el-alert title="消息提示的文案" type="info" />
    <el-alert title="警告提示的文案" type="warning" />
    <el-alert title="错误提示的文案" type="error" />
    <el-alert title="主要提示的文案" type="primary" />
  </div>
</template>

<script lang="ts" setup>
import { Alert as ElAlert } from '@szhn/dh-design-pc'

</script>

<style scoped>
.demo-alert {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
</style>

主题

Alert 提供了 lightdark 两种主题,通过 effect 属性切换。

<template>
  <div class="demo-alert">
    <el-alert title="成功提示的文案" type="success" effect="dark" />
    <el-alert title="消息提示的文案" type="info" effect="dark" />
    <el-alert title="警告提示的文案" type="warning" effect="dark" />
    <el-alert title="错误提示的文案" type="error" effect="dark" />
  </div>
</template>

<script lang="ts" setup>
import { Alert as ElAlert } from '@szhn/dh-design-pc'

</script>

<style scoped>
.demo-alert {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
</style>

自定义关闭按钮

可以通过 closable 控制 Alert 是否可关闭,close-text 用来自定义关闭按钮文本,关闭时触发 close 事件。

<template>
  <div class="demo-alert">
    <el-alert title="不可关闭" type="success" :closable="false" />
    <el-alert title="自定义关闭文案" type="info" close-text="知道了" />
    <el-alert title="监听关闭事件" type="warning" @close="onClose" />
  </div>
</template>

<script lang="ts" setup>
import { Alert as ElAlert } from '@szhn/dh-design-pc'


function onClose() {
  console.log('Alert 已关闭')
}
</script>

<style scoped>
.demo-alert {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
</style>

使用图标

通过 show-icon 属性展示类型对应的图标,使提示信息更易识别。

<template>
  <div class="demo-alert">
    <el-alert title="成功提示" type="success" show-icon />
    <el-alert title="消息提示" type="info" show-icon />
    <el-alert title="警告提示" type="warning" show-icon />
    <el-alert title="错误提示" type="error" show-icon />
  </div>
</template>

<script lang="ts" setup>
import { Alert as ElAlert } from '@szhn/dh-design-pc'

</script>

<style scoped>
.demo-alert {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
</style>

文字居中

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

<template>
  <div class="demo-alert">
    <el-alert title="居中的成功提示" type="success" center show-icon />
    <el-alert title="居中的警告提示" type="warning" center show-icon />
  </div>
</template>

<script lang="ts" setup>
import { Alert as ElAlert } from '@szhn/dh-design-pc'

</script>

<style scoped>
.demo-alert {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
</style>

文字描述

通过 description 属性添加辅助性描述,可以让用户了解更多信息。

<template>
  <div class="demo-alert">
    <el-alert
      title="带描述信息的提示"
      type="success"
      description="这是一段辅助文字,用来对标题进行更详细的说明,超出长度时会自动换行。"
    />
    <el-alert
      title="警告"
      type="warning"
      description="请核对您填写的信息后再继续。"
    />
  </div>
</template>

<script lang="ts" setup>
import { Alert as ElAlert } from '@szhn/dh-design-pc'

</script>

<style scoped>
.demo-alert {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
</style>

带图标和描述

同时配合 show-icondescription,可以获得信息量更完整的提示。

<template>
  <div class="demo-alert">
    <el-alert
      title="成功的提示"
      type="success"
      description="操作已成功,您可以继续后续动作。"
      show-icon
    />
    <el-alert
      title="错误的提示"
      type="error"
      description="操作未能完成,请重试或联系管理员。"
      show-icon
    />
  </div>
</template>

<script lang="ts" setup>
import { Alert as ElAlert } from '@szhn/dh-design-pc'

</script>

<style scoped>
.demo-alert {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
</style>

API

属性

名称说明类型默认值
title标题string
type类型success / warning / info / error / primaryinfo
description描述性文本string
closable是否可以关闭booleantrue
center文字是否居中booleanfalse
close-text自定义关闭按钮文本string
show-icon是否显示类型图标booleanfalse
effect主题样式light / darklight

事件

名称说明
close关闭 Alert 时触发

插槽

名称说明
defaultAlert 内容描述
title标题的内容
icon图标的内容

更多配置参见 Element Plus Alert