Skip to content

Notification 通知提醒

悬浮出现在页面角落,显示全局的通知提醒消息。

基础用法

从页面角落悬浮出现,4.5 秒后自动消失。

vue
<template>
  <ex-button @click="showInfo">信息通知</ex-button>
</template>

<script setup>
import { notification } from 'vue-ex-ui'

const showInfo = () => {
  notification.info({
    title: '信息提示',
    content: '这是一条信息通知',
  })
}
</script>

不同类型

用来显示「成功、警告、消息、错误」类的通知。

vue
<template>
  <ex-button @click="showInfo">信息</ex-button>
  <ex-button type="success" @click="showSuccess">成功</ex-button>
  <ex-button type="warning" @click="showWarning">警告</ex-button>
  <ex-button type="danger" @click="showError">错误</ex-button>
</template>

<script setup>
import { notification } from 'vue-ex-ui'

const showInfo = () => {
  notification.info({
    title: '信息提示',
    content: '这是一条信息通知',
  })
}

const showSuccess = () => {
  notification.success({
    title: '操作成功',
    content: '您的操作已成功完成',
  })
}

const showWarning = () => {
  notification.warning({
    title: '警告',
    content: '请注意,此操作可能有风险',
  })
}

const showError = () => {
  notification.error({
    title: '错误',
    content: '操作失败,请稍后重试',
  })
}
</script>

不同位置

可以设置通知从四个角落弹出。

vue
<template>
  <ex-button @click="showTopRight">右上角</ex-button>
  <ex-button @click="showTopLeft">左上角</ex-button>
  <ex-button @click="showBottomRight">右下角</ex-button>
  <ex-button @click="showBottomLeft">左下角</ex-button>
</template>

<script setup>
import { notification } from 'vue-ex-ui'

const showTopLeft = () => {
  notification.info({
    title: '左上角通知',
    content: '这条通知显示在左上角',
    position: 'top-left',
  })
}

const showBottomRight = () => {
  notification.info({
    title: '右下角通知',
    content: '这条通知显示在右下角',
    position: 'bottom-right',
  })
}
</script>

自定义时长

自定义通知显示时长。

vue
<template>
  <ex-button @click="showCustomDuration">10 秒后关闭</ex-button>
  <ex-button @click="showNonClosable">不自动关闭</ex-button>
</template>

<script setup>
import { notification } from 'vue-ex-ui'

const showCustomDuration = () => {
  notification.info({
    title: '自定义时长',
    content: '这条通知将在 10 秒后关闭',
    duration: 10000,
  })
}

const showNonClosable = () => {
  notification.info({
    title: '不可关闭',
    content: '这条通知不会自动关闭',
    duration: 0,
    closable: false,
  })
}
</script>

HTML 内容

支持渲染 HTML 字符串。

vue
<template>
  <ex-button @click="showHtml">HTML 内容</ex-button>
</template>

<script setup>
import { notification } from 'vue-ex-ui'

const showHtml = () => {
  notification.info({
    title: 'HTML 内容',
    content: '<strong>加粗文本</strong> 和 <em>斜体文本</em>',
    dangerouslyUseHTMLString: true,
  })
}
</script>

安全提示

dangerouslyUseHTMLString 属性会将 content 作为 HTML 片段处理,这可能会导致 XSS 攻击。请确保 content 的内容是可信的。

带回调

可以设置点击和关闭时的回调函数。

vue
<template>
  <ex-button @click="showWithCallback">带回调的通知</ex-button>
</template>

<script setup>
import { notification } from 'vue-ex-ui'

const showWithCallback = () => {
  notification.success({
    title: '带回调的通知',
    content: '点击通知或关闭按钮会触发回调',
    onClick: () => {
      console.log('通知被点击')
    },
    onClose: () => {
      console.log('通知已关闭')
    },
  })
}
</script>

关闭所有

一次性关闭所有通知。

vue
<template>
  <ex-button @click="showInfo">显示通知</ex-button>
  <ex-button type="danger" @click="closeAll">关闭所有</ex-button>
</template>

<script setup>
import { notification } from 'vue-ex-ui'

const showInfo = () => {
  notification.info({
    title: '信息提示',
    content: '这是一条信息通知',
  })
}

const closeAll = () => {
  notification.closeAll()
}
</script>

全局配置

可以通过 notification.config 方法配置全局默认选项。

typescript
import { notification } from 'vue-ex-ui'

// 配置全局默认选项
notification.config({
  duration: 5000,           // 默认显示时长
  offset: 20,               // 距离边缘的偏移量
  maxCount: 3,              // 最大显示数量
  showIcon: true,           // 是否显示图标
  closable: true,           // 是否可关闭
  position: 'top-right',    // 默认位置
})

API

方法

方法名说明参数返回值
notification(options)显示通知NotificationOptionsNotificationInstance
notification.info(options)显示信息通知NotificationOptionsNotificationInstance
notification.success(options)显示成功通知NotificationOptionsNotificationInstance
notification.warning(options)显示警告通知NotificationOptionsNotificationInstance
notification.error(options)显示错误通知NotificationOptionsNotificationInstance
notification.close(id)关闭指定通知string
notification.closeAll()关闭所有通知
notification.config(options)配置全局选项NotificationGlobalConfig

NotificationOptions

参数说明类型可选值默认值
id通知唯一标识string自动生成
type通知类型stringinfo / success / warning / errorinfo
title通知标题string
content通知内容string
showIcon是否显示图标booleantrue
closable是否可关闭booleantrue
duration显示时长(毫秒),设为 0 则不自动关闭number4500
position显示位置stringtop-right / top-left / bottom-right / bottom-lefttop-right
customClass自定义 CSS 类名string
customStyle自定义样式object
zIndex层级number2000
offset距离边缘的偏移量number16
dangerouslyUseHTMLString是否将内容作为 HTML 处理booleanfalse
onClose关闭时的回调() => void
onClick点击时的回调() => void

Slots

插槽名说明
default通知内容
title自定义标题
icon自定义图标
close自定义关闭按钮

NotificationInstance

属性/方法说明类型
id通知唯一标识string
close关闭通知() => void

NotificationGlobalConfig

参数说明类型默认值
duration默认显示时长number4500
offset默认偏移量number16
zIndex默认层级number2000
maxCount最大显示数量number5
showIcon是否显示图标booleantrue
closable是否可关闭booleantrue
position默认位置stringtop-right

最佳实践

使用场景

  • info:用于普通的信息通知
  • success:用于操作成功后的反馈
  • warning:用于警告用户注意某些事项
  • error:用于操作失败后的错误提示

与 Message 的区别

特性MessageNotification
显示位置页面顶部居中页面四个角落
内容复杂度简单文本标题 + 描述
使用场景轻量级反馈详细通知
默认时长3 秒4.5 秒

无障碍

  • 通知组件使用 role="alert" 属性,屏幕阅读器会自动朗读通知内容
  • 错误通知使用 aria-live="assertive",会立即打断当前朗读
  • 其他类型通知使用 aria-live="polite",会等待当前朗读完成
  • 关闭按钮支持键盘操作(Enter 和 Space 键)