Notify
Page-scoped banner notification; functions use the page host and isolate replaced notification handles.
Import
Use <hui-notify> after configuring uni-app easycom. Import public types and functions from @nscode/handy-uni. Ordinary Vue applications install the @nscode/handy-uni/web preview plugin.
Examples
Each example is standalone. Controls show the current value or event result on the page.
<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
| Prop | Type | Default | Details |
|---|---|---|---|
visible | boolean | undefined | Controlled visibility. |
defaultVisible | boolean | false | Initial uncontrolled visibility. |
message | string | — | Notification body; newline characters are preserved. |
type | NotifyType | 'danger' | Semantic visual variant. |
duration | number | 3000 | Animation or display duration in milliseconds; see platform notes for zero. |
icon | string | — | Icon registry name. |
color | string | — | Explicit text color. |
bgColor | string | — | Explicit background color. |
closable | boolean | false | Displays a close control. |
offsetTop | number | — | Offset below the viewport top; defaults to the page-host navigation-bar bottom. |
Root classes and styles use customClass and customStyle. Numbers representing dimensions use px; durations use ms unless explicitly documented.
Events
| Event | Arguments |
|---|---|
update:visible | (visible: boolean) |
click | (event: unknown) |
close | (reason: NotifyCloseReason) |
closed | () |
Controlled state only changes when the parent accepts its update event. Events with multiple words use kebab-case. Disabled or stale asynchronous actions do not emit a successful state change.
Slots
| Slot | Scope |
|---|---|
default | — |
Methods
None.
showNotify(options: NotifyOptions \| string): NotifyHandle;closeNotify(). The returned handle exposes close() and update(patch); stale handles are inert.
Types
NotifyType、NotifyCloseReason、NotifyOptions、NotifyHandle、NotifyProps、NotifyEmits.
Platform notes
Each page has one function notification; replacing it invokes the previous onClose. Stale handles cannot change newer notifications and update does not restart its timer. The persistent page host owns the live region and positions banners above page layers. Missing hosts are retried on the next task before falling back to a native toast.
Empty messages do not display and emit a development warning. An empty function request preserves the existing notification; updating its message to empty closes it. Standalone components can supply content through the default slot. All three paths replace negative or non-finite durations with 3000ms and warn; duration=0 persists. Verify the slide-in animation and screen-reader announcements on devices.
Device smoke checklist.