通知
四角通知,支持自动关闭、独立堆叠、操作按钮和 Chalk/OEM 主题。
默认导出是组件插件,具名 Notification 是可调用服务。安装总包或 app.use(NfNotification) 后可使用 this.$openNotify。setup 中推荐 useNotification(),继承当前 provider 的注入和语言,并在组件卸载时清理通知;也可用 createNotification(appContext) 创建应用专属服务。直接 Notification 不隐式绑定最后安装的应用,可用第二参数显式传入 appContext。
基础用法与四角定位
通知类型
取消与确认
文本、HTML 与 VNode
组件与默认插槽
交互体验
按需引入
import NfNotification, { Notification, useNotification, createNotification } from '@94ai-plus/nf-notification'
import '@94ai-plus/nf-theme-chalk/nf-notification.css'
app.use(NfNotification)
Notification.success({ title: 'Saved', message: 'Success' })
const notify = createNotification(app._context)
notify.info('App context')
// setup(): const notify = useNotification()属性
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| title | 标题 | string | — | '' |
| message | 说明文字 | string / VNode | — | '' |
| dangerouslyUseHTMLString | 是否将 message 属性作为 HTML 片段处理,仅用于可信内容 | boolean | — | false |
| type | 主题样式,如果不在可选值内将被忽略 | string | success / warning / info / error | '' |
| iconClass | 自定义图标的类名。若设置了有效的 type,则 iconClass 会被覆盖 | string | — | '' |
| customClass | 自定义类名 | string | — | '' |
| duration | 显示时间,毫秒。设为 0 则不会自动关闭 | number | — | 4500 |
| position | 自定义弹出位置 | string | top-right / top-left / bottom-right / bottom-left | top-right |
| showClose | 是否显示关闭按钮 | boolean | — | true |
| onClose | 关闭时的回调函数,参数为被关闭实例的公开 handler | (handler: NotificationHandler) => void | — | — |
| onClick | 点击 Notification 时的回调函数 | () => void | — | — |
| offset | 偏移的距离,同一时刻所有 Notification 实例应具有相同偏移量;另含 16px 窗口边距 | number | — | 0 |
| cancelButtonText | 取消按钮的文本内容 | string | — | '' |
| confirmButtonText | 确定按钮的文本内容 | string | — | '' |
| onCancel | 点击取消按钮时的回调函数,不自动关闭通知 | () => void | — | — |
| onConfirm | 点击确定按钮时的回调函数,不自动关闭通知 | () => void | — | — |
| zIndex | 层级,服务调用默认从 3001 起递增 | number | — | 自动递增 |
事件
| 事件名称 | 说明 | 回调参数 |
|---|---|---|
| click | 点击 Notification 时的回调函数 | — |
| cancel | 点击取消按钮时的回调函数,不自动关闭通知 | — |
| confirm | 点击确定按钮时的回调函数,不自动关闭通知 | — |
| close | 关闭时触发 | (handler: NotificationHandler) |
| destroy | 离场动画结束后触发 | — |
插槽
| Slot | 说明 | 回调参数 |
|---|---|---|
| default | 正文内容,优先于 message | — |
方法与实例
| API | Description |
|---|---|
| Notification(string / VNode / options, appContext?) | Returns NotificationHandler |
| Notification.success / warning / info / error(options?) | Type shortcuts; accept string, VNode or options |
| handler.close() | 关闭当前的 Notification,重复调用仅关闭一次 |
| Notification.close(id, onClose?) | Close by id; identical callback deduplicated |
| Notification.closeAll() | Close a snapshot of active notifications |
| createNotification(appContext) / useNotification() | Context-aware scoped service |
迁移说明
保留旧版默认 4500ms、四角独立堆叠、16px 边距和间距、悬停暂停与移出完整重新计时;Delete/Backspace 暂停,其他按键重新计时,Escape 关闭通知。修复键盘重复计时器;悬停期间不会被按键意外启动。高度变化、窗口缩放和中间通知关闭会重新布局。
取消/确认按钮仅调用回调,不自动关闭,也不触发卡片 onClick;如需关闭,请在回调中调用 handler.close()。有效 type 图标优先于 iconClass;无 type 且无 iconClass 时不显示图标。标题和普通字符串默认转义,HTML 模式仅用于可信内容。修复无 message 时默认插槽不显示的问题;业务正文与按钮文本由业务提供,关闭按钮 aria-label 支持八语言。
close() 幂等,close(id) 现在真正关闭 DOM,清理定时器、监听器和观察器。onClose 接收 NotificationHandler(id、position、el/$el/dom、visible、closed、close),不再暴露 Vue 2 私有实例;SSR 返回 closed=true 的空操作 handler。应用专属服务的 close/closeAll 只处理自身通知,直接 Notification.closeAll() 处理页面所有通知。Message 与 Notification 的记录独立。通知挂载到 body,主题跟随根 data-nf-theme;声明式组件不参与服务堆叠。
