Toast 轻提示
通过页面宿主显示短暂提示、成功失败状态及持续加载。
引入
uni-app 配置 easycom 后使用 <hui-toast>;公开类型从 @nscode/handy-uni 导入。普通 Vue 3 项目安装 @nscode/handy-uni/web 预览版插件后使用同名组件。
示例
操作控件后,页面直接显示当前值和事件结果。
vue
<template>
<view>
<hui-page-host
><template #toast><hui-toast /></template></hui-page-host
><hui-button @click="notify">显示提示 / Notify</hui-button
><hui-button @click="loading">持续加载 / Loading</hui-button
><hui-button @click="stop">关闭 / Close</hui-button>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { showToast, showLoadingToast } from '@nscode/handy-uni'
const lastEvent = ref('尚无事件')
let handle: ReturnType<typeof showToast> | undefined
const notify = () => {
handle = showToast({ message: '保存成功 / Saved', type: 'success', duration: 1500 })
}
const loading = () => {
handle = showLoadingToast({ message: '处理中 / Working', forbidClick: true })
}
const stop = () => handle?.close()
</script>
每页放一个 hui-page-host 即可,由宿主常驻播报区。函数返回 close/update 句柄;文字更新不重置计时,显式更新 duration 才重设。duration=0 持续显示。
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|
根节点属性见通用属性。布尔默认值为 undefined 时支持继承;显式 false 会覆盖继承值。其他布尔值按表中默认值处理。
Events
无。
Slots
无。
方法
无。
函数 API
| 函数 | 说明 |
|---|---|
showToast(options | message) | 显示提示,默认 2000ms。 |
showLoadingToast(options | message) | 加载提示,默认持续显示且禁止背景点击。 |
showSuccessToast(options | message) | 成功提示。 |
showFailToast(options | message) | 失败提示。 |
closeToast() | 关闭当前函数式提示。 |
显示函数均返回 ToastHandle。新提示替换旧提示;旧句柄不能关闭或修改新提示。层号取实际展示页面的最高弹层 + 1,关闭弹层的离场动画期间仍保留层栈顺序。提示内容使用 --hui-toast-bg,播报区常驻页面宿主。
ts
import { closeToast, showLoadingToast, showSuccessToast } from '@nscode/handy-uni'
const task = showLoadingToast({ message: '正在保存' })
task.update({ message: '保存进度 50%' }) // 只改文字,不重置计时
task.close()
showSuccessToast({ message: '保存成功', duration: 1500 })
closeToast()
类型
ToastProps、ToastType、ToastPosition、ToastOptions、ToastHandle 可从 @nscode/handy-uni 导入。
ts
export type ToastType = 'text' | 'success' | 'fail' | 'warning' | 'loading'
export type ToastPosition = 'top' | 'middle' | 'bottom'
export interface ToastOptions {
message?: string
type?: ToastType
icon?: string
duration?: number
position?: ToastPosition
forbidClick?: boolean
onClose?: () => void
}
export interface ToastHandle {
close(): void
update(patch: Partial<ToastOptions>): void
}
平台说明
| 行为 | uni-app H5 | 微信小程序 | App-vue | 普通 Vue 3 Web(预览) |
|---|---|---|---|---|
| 基本交互 | 已实现,待设备复核 | 已实现,待设备复核 | 已实现,待设备复核 | 由 Web 平台适配层实现 |
| 提示层和播报 | 本页最高弹层 + 1;常驻播报区 | 同 H5,读屏待验 | 与原生层叠及读屏待验 | 自动宿主可用;常驻 role=status/aria-live 区域 |
设备操作见冒烟清单。真实设备读数单独记录,单元测试通过不代表真机验收。