Skip to content

Toast 轻提示

轻量级的全局通知提示,不打断用户流程,自动消失。

使用前准备

推荐在主页面或业务根布局使用 LkRoot。Root 会自动挂载 Toast 宿主:

vue
<template>
  <lk-root>
    <page-layout />
  </lk-root>
</template>

如果暂不使用 LkRoot,也可以直接放置低层 Toast 管理器:

vue
<template>
  <view>
    <page-layout />
    <lk-toast-manager />
  </view>
</template>

同一页面只保留一个 Toast 宿主。使用 LkRoot 默认配置时,不要再额外挂载 <lk-toast-manager />

基础用法

vue
<script setup lang="ts">
import { toastStore } from '@/uni_modules/lucky-ui/components/lk-toast/toast-manager'

function showMsg() {
  toastStore.show('操作成功!')
}
</script>

<template>
  <lk-button @click="showMsg">显示 Toast</lk-button>
</template>

不同位置

vue
<script setup lang="ts">
import { toastStore } from '@/uni_modules/lucky-ui'

function top() { toastStore.show({ message: '顶部提示', position: 'top' }) }
function center() { toastStore.show({ message: '居中提示', position: 'center' }) }
function bottom() { toastStore.show({ message: '底部提示', position: 'bottom' }) }
</script>

<template>
  <view class="demo-row">
    <lk-button @click="top">顶部</lk-button>
    <lk-button @click="center">居中</lk-button>
    <lk-button @click="bottom">底部</lk-button>
  </view>
</template>

自定义时长

vue
<script setup lang="ts">
import { toastStore } from '@/uni_modules/lucky-ui'

function show1s() { toastStore.show({ message: '显示 1 秒', duration: 1000 }) }
function show4s() { toastStore.show({ message: '显示 4 秒', duration: 4000 }) }
function stay() { toastStore.show({ message: '不自动关闭', duration: 0 }) }
</script>

<template>
  <view class="demo-row">
    <lk-button @click="show1s">1 秒</lk-button>
    <lk-button @click="show4s">4 秒</lk-button>
    <lk-button @click="stay">不关闭</lk-button>
  </view>
</template>

动画效果

vue
<script setup lang="ts">
import { toastStore } from '@/uni_modules/lucky-ui'
</script>

<template>
  <view class="demo-row">
    <lk-button @click="toastStore.show({ message: 'slide-up', transition: 'slide-up' })">
      SlideUp
    </lk-button>
    <lk-button @click="toastStore.show({ message: 'fade', transition: 'fade' })">
      Fade
    </lk-button>
    <lk-button @click="toastStore.show({ message: 'zoom', transition: 'zoom' })">
      Zoom
    </lk-button>
  </view>
</template>

手动关闭

vue
<script setup lang="ts">
import { toastStore } from '@/uni_modules/lucky-ui'
import { ref } from 'vue'

const toastId = ref<number | null>(null)

function open() {
  toastId.value = toastStore.show({ message: '长时间 Toast,点击关闭', duration: 0 })
}

function close() {
  if (toastId.value !== null) {
    toastStore.close(toastId.value)
    toastId.value = null
  }
}
</script>

<template>
  <lk-button @click="open">打开</lk-button>
  <lk-button variant="outline" @click="close">关闭</lk-button>
</template>

API

toastStore 方法

方法说明参数返回值
show显示 Toaststring | ToastOptionsid: number
close关闭指定 Toast(带退出动画)(id: number) => void
clear关闭所有 Toast() => void

ToastOptions

参数说明类型默认值
customClass组件可视根节点自定义类名string | object | array''
customStyle组件可视根节点自定义样式string | object''
message提示内容string
duration自动关闭时长(ms),0 表示不关闭number2000
position显示位置top | center | bottomcenter
transition动画效果slide-up | fade | zoomslide-up

LkToastManager 组件

参数说明类型默认值
customClass组件可视根节点自定义类名string | object | array''
customStyle组件可视根节点自定义样式string | object''

Events

事件名说明回调参数
update:modelValue显隐状态变化,用于 v-model(value: boolean)
openToast 显示时触发()
closeToast 关闭时触发()
after-leave离开动画结束后触发()

发布验收

lk-toast 已纳入 needs-hardening showcase 回归,发布前按下面边界验收:

场景验收方式要点
展示台基线自动回归tests/visual/needs-hardening-showcase.spec.ts 校验组件路由、verified 状态与中风险标记
全局管理自动/人工多个 Toast 连续触发时 id、关闭动画和 clear() 行为稳定
定位与时序人工验收top/center/bottom 与自动关闭时长在 H5/App/小程序端一致

基于 MIT 协议开源 · 粤ICP备2022114240号-3

Toast 轻提示

正在连接预览服务...

请先运行 pnpm run dev:h5