Notify 消息通知
页面级横幅通知;函数调用使用页面宿主,替换通知与旧句柄相互隔离。
引入
uni-app 配置 easycom 后使用 <hui-notify>。公开类型和函数从 @nscode/handy-uni 导入;普通 Vue 3 安装 @nscode/handy-uni/web 预览插件。
示例
示例可独立运行;交互结果直接显示在页面。
vue
<template>
<view>
<hui-space direction="vertical" fill>
<hui-page-host custom-class="hui-space__item" />
<hui-button custom-class="hui-space__item" @click="notify"
>显示通知 / Show notification</hui-button
>
<hui-button custom-class="hui-space__item" @click="closeNotify">关闭 / Close</hui-button>
<hui-notify
v-model:visible="visible"
custom-class="hui-space__item"
message="独立通知 / Standalone notice"
type="success"
:duration="0"
closable
@close="(reason) => (lastEvent = 'close:' + reason)"
@click="(event) => (lastEvent = 'click:' + String(event))"
@closed="lastEvent = 'closed'"
/>
<hui-button custom-class="hui-space__item" @click="visible = !visible"
>独立模式 / Standalone</hui-button
><text class="hui-space__item">{{ lastEvent }}</text>
</hui-space>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
import { showNotify, closeNotify } from '@nscode/handy-uni'
const visible = ref(false)
const notify = () =>
showNotify({
message: '保存成功 / Saved',
type: 'success',
duration: 3000,
onClose: () => {
lastEvent.value = 'closed'
}
})
</script>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
visible | boolean | undefined | 受控可见状态,使用 v-model:visible 接收更新。 |
defaultVisible | boolean | false | 非受控模式的初始可见状态,仅初始化读取。 |
message | string | — | 文字,支持 \n |
type | NotifyType | 'danger' | 组件默认都是 danger:顶部横幅最常见的用法是报错 |
duration | number | 3000 | ms;0 不自动关 |
icon | string | — | 文字前的图标;默认不显示 |
color | string | — | 文字色,优先于 type |
bgColor | string | — | 背景色,优先于 type |
closable | boolean | false | 右侧关闭按钮 |
offsetTop | number | — | 距窗口顶部,px;不传时贴在页面宿主登记的导航栏底边下面,没登记时为 0(H5 另加 --window-top) |
根节点类名和样式使用 customClass、customStyle。尺寸数字按 px,时长除明确说明外按 ms。
Events
| 事件 | 参数 |
|---|---|
update:visible | (visible: boolean) |
click | (event: unknown) |
close | (reason: NotifyCloseReason) |
closed | () |
受控状态由父级回写更新事件后改变。多词事件使用短横线。禁用操作、取消及过期异步结果不提交成功变更。
Slots
| 插槽 | 作用域 |
|---|---|
default | — |
方法
无。
showNotify(options: NotifyOptions \| string): NotifyHandle;closeNotify(). 返回句柄包含 close() 和 update(patch),已关闭或被替换的旧句柄无效。
类型
NotifyType、NotifyCloseReason、NotifyOptions、NotifyHandle、NotifyProps、NotifyEmits.
平台说明
每页仅一条函数通知;新通知替换旧通知并调用旧 onClose。旧句柄不能操作新通知,update 不重新计时。页面宿主常驻播报区,层号为本页最高弹层之上。无宿主延后一宏任务再尝试,仍没有才降级原生提示。
消息为空时不显示并在开发环境警告;空的函数请求不会替换已有通知,更新为空会关闭当前通知。独立组件可使用默认插槽提供内容。三种调用方式均将负数或非有限的 duration 回退到 3000ms 并警告;duration=0 不自动关。横幅从顶部滑入,无障碍播报和动画需设备复核。
设备冒烟清单.