Toast 轻提示

在页面中间弹出提示,用于消息通知、加载提示、操作结果提示等场景。

代码演示

基础用法

<!-- 在页面内添加对应的节点 -->
<hd-toast ref="toast"></hd-toast>

<button type="primary" @click="open">开启toast</button>
<script lang="ts" setup>
import { ref } from 'vue'
import { Toast } from '@/uni_modules/fant-mini-plus/components/hd-toast/types'

const toast = ref<Toast>() // toast ref

function open() {
  toast.value?.showToast({
    title: '打开了',
    onClose() {
      console.log('关闭了')
    }
  })
}
</script>

底色类型

通过type设置底色类型;white底色为白色,图标为多色;black底色为黑色,图标为白色;默认为black

<button type="primary" @click="open">开启toast</button>
<hd-toast ref="toast"></hd-toast>
<script lang="ts" setup>
import { ref } from 'vue'
import { Toast } from '@/uni_modules/fant-mini-plus/components/hd-toast/types'

const toast = ref<Toast>() // toast ref

function open() {
  toast.value?.showToast({
    title: '打开了',
    type: 'white',
    onClose() {
      console.log('关闭了')
    }
  })
}
</script>

提示内容

通过title设置提示内容。

<button type="primary" @click="open">开启toast</button>
<hd-toast ref="toast"></hd-toast>
<script lang="ts" setup>
import { ref } from 'vue'
import { Toast } from '@/uni_modules/fant-mini-plus/components/hd-toast/types'

const toast = ref<Toast>() // toast ref

function open() {
  toast.value?.showToast({
    title: '这里是提示内容',
    onClose() {
      console.log('关闭了')
    }
  })
}
</script>

提示图标

通过icon设置提示图标;success: 显示成功图标;warning: 显示警告图标;error: 显示错误图标;none: 不显示图标。

<button type="primary" @click="open">开启toast</button>
<hd-toast ref="toast"></hd-toast>
<script lang="ts" setup>
import { ref } from 'vue'
import { Toast } from '@/uni_modules/fant-mini-plus/components/hd-toast/types'

const toast = ref<Toast>() // toast ref

function open() {
  toast.value?.showToast({
    title: '这里是提示内容',
    icon: 'error',
    onClose() {
      console.log('关闭了')
    }
  })
}
</script>

自定义图标

通过image设置自定义图标,优先级大于icon

<button type="primary" @click="open">开启toast</button>
<hd-toast ref="toast"></hd-toast>
<script lang="ts" setup>
import { ref } from 'vue'
import { Toast } from '@/uni_modules/fant-mini-plus/components/hd-toast/types'

const toast = ref<Toast>() // toast ref

function open() {
  toast.value?.showToast({
    title: '这里是提示内容', icon: 'error', image: '' ,
    onClose() {
      console.log('关闭了')
    }
  })
}
</script>

提示持续时间

通过duration设置调整显示toast时间,单位毫秒,默认 1500 毫秒。

<button type="primary" @click="open">开启toast</button>
<hd-toast ref="toast"></hd-toast>
<script lang="ts" setup>
import { ref } from 'vue'
import { Toast } from '@/uni_modules/fant-mini-plus/components/hd-toast/types'

const toast = ref<Toast>() // toast ref

function open() {
  toast.value?.showToast({
    title: '这里是提示内容', icon: 'error', duration: 2000,
    onClose() {
      console.log('关闭了')
    }
  })
}
</script>

方法

fant-mini 中导出了以下 Toast 相关的辅助函数:

方法名说明参数返回值
showToast展示提示ToastOptionsToast实例
hideToast关闭提示--

ToastOptions 数据结构

调用 showToast 方法时,支持传入以下选项:

参数说明类型是否必填默认值
type底色类型,可选值:white blackToastTypefalseblack
title提示的内容。Stringfalse-
icon图标类型,可选值:none success warning errorToastIconTypefalsenone
image自定义图标的本地路径Stringfalse-
duration提示的持续时间Numberfalse1500

Props

NameDescriptionTypeRequiredDefault
valuetoast是否展示Booleanfalsefalse
durationtoast展示时长(ms),值为 0 时,notify 不会消失,默认值1500Numberfalse1500
titletoast提示的内容Stringfalse-
typetoast底色类型'white' / 'black'false默认值是:black
icontoast图标 - success: 显示成功图标 - warning: 显示警告图标 - error: 显示错误图标 - none: 不显示图标'success' / 'none'/ 'warning'/ 'error'false默认值是:none
imagetoast自定义图片Stringfalse-
zIndex自定义层级,默认值 1000Numberfalse1000

Events

Event NameDescriptionParameters
update:visible消息展示状态变更时触发value:Boolean 消息展示状态,建议通过v-model双向绑定输入值,而不是监听此事件的形式
被点击被点击时触发-