Skip to content

Notification 通知

Notification 悬浮出现在页面角落,用于展示全局通知提醒。VISTA B 直接使用 Element Plus 的 ElNotification 服务,适合系统任务结果、后台识别进度、风险提醒等需要脱离当前内容流展示的信息。

基础用法

ElNotification 接受一个对象作为参数。通过 title 设置标题,通过 message 设置正文;默认 4500 毫秒后自动关闭,duration 设置为 0 时不会自动关闭。

vue
<script setup>
import { h } from 'vue'
import { ElNotification } from 'element-plus'

const open1 = () => {
  ElNotification({
    title: 'Title',
    message: h('i', { style: 'color: teal' }, 'This is a reminder')
  })
}

const open2 = () => {
  ElNotification({
    title: 'Prompt',
    message: 'This is a message that does not automatically close',
    duration: 0
  })
}
</script>

<template>
  <el-button plain @click="open1">Closes automatically</el-button>
  <el-button plain @click="open2">Won't close automatically</el-button>
</template>

不同类型

Notification 支持 primarysuccesswarninginfoerror 五种类型。也可以使用 ElNotification.success(options) 这类类型方法。

vue
<script setup>
import { ElNotification } from 'element-plus'

const open = () => {
  ElNotification({
    title: 'Success',
    message: 'This is a success message',
    type: 'success'
  })
}

const openByMethod = () => {
  ElNotification.warning({
    title: 'Warning',
    message: 'This is a warning message'
  })
}
</script>

自定义消息弹出的位置

通过 position 设置弹出位置,支持 top-righttop-leftbottom-rightbottom-left,默认值为 top-right

vue
<script setup>
import { ElNotification } from 'element-plus'

const open = () => {
  ElNotification({
    title: 'Custom Position',
    message: "I'm at the bottom right corner",
    position: 'bottom-right'
  })
}
</script>

有位置偏移的通知栏

通过 offset 设置通知距屏幕边缘的偏移量。同一时刻同一方向的 Notification 实例建议使用相同偏移量。

vue
<script setup>
import { ElNotification } from 'element-plus'

const open = () => {
  ElNotification.success({
    title: 'Success',
    message: 'This is a success message',
    offset: 100
  })
}
</script>

使用 HTML 片段作为正文内容

dangerouslyUseHTMLString 设置为 true 后,message 会被当作 HTML 片段处理。

vue
<script setup>
import { ElNotification } from 'element-plus'

const open = () => {
  ElNotification({
    title: 'HTML String',
    dangerouslyUseHTMLString: true,
    message: '<strong>This is <i>HTML</i> string</strong>'
  })
}
</script>

WARNING

开启 dangerouslyUseHTMLString 后,只能传入可信内容,不要将用户提交的内容直接赋值给 message

函数形式的 message

message 可以是 VNode。若 VNode 中包含动态属性,建议使用函数形式返回 VNode。

vue
<script setup>
import { h, ref } from 'vue'
import { ElNotification, ElSwitch } from 'element-plus'

const open = () => {
  ElNotification({
    title: 'Use VNode',
    message: h('p', null, [
      h('span', null, 'Message can be '),
      h('i', { style: 'color: teal' }, 'VNode')
    ])
  })
}

const openDynamic = () => {
  const checked = ref(false)

  ElNotification({
    title: 'Use VNode',
    message: () =>
      h(ElSwitch, {
        modelValue: checked.value,
        'onUpdate:modelValue': (value) => {
          checked.value = value
        }
      })
  })
}
</script>

隐藏关闭按钮

showClose 设置为 false 可以隐藏关闭按钮。

vue
<script setup>
import { ElNotification } from 'element-plus'

const open = () => {
  ElNotification.success({
    title: 'Info',
    message: 'This is a message without close button',
    showClose: false
  })
}
</script>

单独引用

在处理函数内调用 ElNotification(options) 可以呼出通知栏。ElNotification 返回当前通知实例,实例提供 close 方法;需要关闭所有通知时,可以调用 ElNotification.closeAll()

等待触发
vue
<script setup>
import { ElNotification } from 'element-plus'
import { CloseBold } from '@element-plus/icons-vue'

const open = () => {
  const notification = ElNotification({
    title: 'Title',
    message: 'This is a message',
    closeIcon: CloseBold
  })

  notification.close()
}

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

API

配置项

名称说明类型默认值
title标题string''
message通知栏正文内容string / VNode / Function''
dangerouslyUseHTMLString是否将 message 作为 HTML 片段处理booleanfalse
type通知类型primary / success / info / warning / error / ''''
icon自定义图标。设置 type 时会被覆盖string / Component-
customClass自定义类名string''
duration显示时间,单位毫秒;设为 0 时不会自动关闭number4500
position弹出位置top-right / top-left / bottom-right / bottom-lefttop-right
showClose是否显示关闭按钮booleantrue
onClose关闭时的回调函数(vm: VNode) => void-
onClick点击 Notification 时的回调函数() => void-
offset相对屏幕边缘的偏移量number0
appendTo设置通知栏根元素CSSSelector / HTMLElementdocument.body
zIndex初始 z-indexnumber0
closeIcon自定义关闭图标string / ComponentClose

方法

名称说明类型
close关闭当前 Notification 实例() => void
ElNotification.closeAll关闭所有 Notification 实例() => void
ElNotification.updateOffsets手动更新指定方向的通知偏移(position?: NotificationPosition) => void

使用提醒

  • Notification 用于全局通知,不用于表单字段旁的局部校验提示。
  • duration: 0 的通知需要明确关闭路径,避免用户界面长期堆积。
  • dangerouslyUseHTMLString 只用于可信静态内容;动态内容优先使用 VNode。

HOMEVISTA 设计规范