Skip to content

Notification 通知 ​

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

ts
import { Notification } from '@szhn/dh-design-pc'

基础用法 ​

Notification 组件提供通知功能,调用时接受一个对象作为参数。可通过 title 和 message 设置标题和正文,duration 控制自动关闭时长,设为 0 表示不会自动关闭。

不同类型的通知 ​

dh-design-pc 为 Notification 组件准备了 success / warning / info / error 四种类型,可以通过 type 字段指定,也可以直接使用 Notification.success 等快捷方法。

自定义弹出位置 ​

使用 position 属性可让 Notification 从屏幕四角中的任意一角弹出,支持 top-right / top-left / bottom-right / bottom-left,默认为 top-right。

有位置偏移的通知栏 ​

通过 offset 字段可以使弹出的消息距屏幕边缘偏移一段距离。

使用 HTML 片段 ​

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

使用 VNode ​

message 既支持普通 VNode,也支持返回 VNode 的函数形式,适合承载带状态的内容。

隐藏关闭按钮 ​

将 showClose 设置为 false 可以隐藏通知右上角的关闭按钮。

全局方法 ​

如果以插件方式完整注册,组件实例上会自动注入 $notify 方法。

按需引入 ​

ts
import { Notification } from '@szhn/dh-design-pc'

Notification({
  title: '提示',
  message: '这是一条通知消息',
})
Notification.closeAll()

应用上下文继承 ​

通知方法支持接收当前应用的 appContext 作为第二个参数,以继承全局上下文能力。

ts
import { getCurrentInstance } from 'vue'
import { Notification } from '@szhn/dh-design-pc'

const { appContext } = getCurrentInstance()!
Notification({}, appContext)

API ​

配置项(部分) ​

名称说明类型默认
title标题string''
message正文string / VNode / Function''
type通知类型success / warning / info / error / primary''
icon自定义图标string / Component—
duration显示时间(毫秒),0 表示不自动关闭number4500
position弹出位置top-right / top-left / bottom-right / bottom-lefttop-right
showClose是否显示关闭按钮booleantrue
offset相对屏幕的偏移量number0
appendTo指定挂载根节点string / HTMLElement—
dangerouslyUseHTMLString是否作为 HTML 片段booleanfalse
onClose关闭时回调Function—
onClick点击时回调Function—
closeIcon自定义关闭图标string / ComponentClose

方法 ​

名称说明
Notification.success / warning / info / error快捷打开对应类型的通知
Notification.closeAll()关闭所有通知
instance.close()关闭当前通知

更多配置参见 Element Plus Notification。