Notification 通知
在页面角落展示全局通知。
基础用法
Uniboot UI 注册全局 $notify,参数为对象。最简单可设置 title 与 message。默认 4500ms 后自动关闭;duration 控制停留时间,设为 0 则不自动关闭。duration 单位为毫秒。
带类型
提供成功、警告、信息与错误等类型。
通过 type 设置类型:primary、success、warning、info、error,其他值忽略。亦可直接调用类型方法(如示例中的 open3、open4)而无需传 type。
自定义位置
可从任意角落滑入。
position 取 top-right、top-left、bottom-right、bottom-left,默认 top-right。
偏移量
自定义与屏幕边缘的距离。
使用 offset 设置偏移;同一时刻多个实例建议使用相同 offset。
使用 HTML 字符串
message 支持 HTML 字符串。
dangerouslyUseHTMLString 为 true 时按 HTML 解析 message。
WARNING
虽然 message 支持 HTML,但动态渲染不可信 HTML 易导致 XSS。启用 dangerouslyUseHTMLString 时请确保内容可信,切勿将用户输入直接作为 message。
函数式 message
message 可为 VNode。
message 支持返回 VNode 的函数。
隐藏关闭按钮
可隐藏关闭按钮。
showClose 为 false 时用户无法点击关闭。
全局方法
完整引入后会在 app.config.globalProperties 注册 $notify,选项式 API 中可如本文调用。
按需引入
import { UNotification } from 'uniboot-ui'
import { CloseBold } from '@uniboot/icons-vue'
UNotification({
title: 'Title',
message: 'This is a message',
closeIcon: CloseBold,
})使用 UNotification(options),亦提供 UNotification.success(options) 等快捷方法。可调用 UNotification.closeAll() 关闭全部实例;可调用 UNotification.updateOffsets(position) 手动更新某方向上所有实例的偏移。
应用上下文继承
构造函数第二个参数可传入 context,以继承当前应用上下文。
TIP
若已全局注册 UNotification,将自动继承应用上下文。
import { getCurrentInstance } from 'vue'
import { UNotification } from 'uniboot-ui'
// 在 setup 中
const { appContext } = getCurrentInstance()!
UNotification({}, appContext)通知 API
选项
| 名称 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| title | 标题 | string | '' |
| message | 描述文本 | string / VNode / Function | '' |
| dangerouslyUseHTMLString | message 是否按 HTML 解析 | boolean | false |
| type | 类型 | enum | '' |
| icon | 自定义图标(会被 type 覆盖) | string / Component | — |
| customClass | 自定义类名 | string | '' |
| duration | 自动关闭前停留时间,0 表示不自动关闭 | number | 4500 |
| position | 位置 | enum | top-right |
| showClose | 是否显示关闭按钮 | boolean | true |
| onClose | 关闭时回调 | Function | — |
| onClick | 点击通知时回调 | Function | — |
| offset | 距屏幕顶边偏移;同一时刻多个实例建议使用相同值 | number | 0 |
| appendTo | 挂载根元素,默认 document.body | CSSSelector / HTMLElement | — |
| zIndex | 初始 z-index | number | 0 |
| closeIcon | 自定义关闭图标 | string / Component | Close |
方法
Notification 与 this.$notify 返回当前实例,可调用 close 手动关闭。
| 名称 | 说明 | 类型 |
|---|---|---|
| close | 关闭通知 | Function |