Toast 轻提示 APP
该方法或组件支持移动端小程序调用
PC
该方法或组件支持桌面端小程序调用

一种轻量级反馈/提示,可以用来显示不会打断用户操作的内容,适合用于页面转场、数据交互的等场景中。

规则

  • 一次只显示一个 toast。
  • 有 Icon 的 Toast,字数为 4-6 个;没有 Icon 的 Toast,字数不宜超过 14 个。

引入方式

数字生活小程序工程中,不需要 import,直接使用组件标签即可。

import { Toast } from '@21cnfe/vui'  
Vue.$Toast = Vue.prototype.$Toast = Toast

代码演示

基本用法 type为info,展示纯文本的信息,onClose为toast关闭后回调方法
<template>
<Button @click="showInfoToast">Info</Button>
</template>
<script>
export default {
methods: {
showTextToast () {
this.$Toast.info({content: '文本信息', duration: 1, onClose: this.handleClose})
},
handleClose () {
console.log('toast消失自定义回调函数')
}
}
}
</script>
带有倾向性 带有 icon,常用来显示「成功、失败、错误、加载」类的系统消息
<template>
<Button @click="showSuccessToast">success</Button>
<Button @click="showFailToast">fail</Button>
<Button @click="showNetToast">network failure</Button>
<Button @click="showLoadToast">loading</Button>
</template>
<script>
export default {
methods: {
showSuccessToast () {
console.log(this.$Toast)
this.$Toast.success({content: '成功文本信息', duration: 2, onClose: this.handleClose})
},
showFailToast () {
console.log(this.$Toast)
this.$Toast.fail({content: '失败文本信息', duration: 3, onClose: this.handleClose})
},
showNetToast () {
console.log(this.$Toast)
this.$Toast.offline({content: '网络错误文本信息', duration: 4, onClose: this.handleClose})
},
showLoadToast () {
console.log(this.$Toast)
this.$Toast.loading({content: '加载中文本信息', duration: 5, onClose: this.handleClose})
},
handleClose () {
console.log('toast消失自定义回调函数')
}
}
}
</script>
手动关闭 配置 duration 为 null 时不自动关闭,调用 hide 方法手动关闭
<template>
<Button @click="showLoadToast">Loading</Button>
</template>
<script>
export default {
methods: {
showLoadToast () {
this.$Toast.loading({duration: null, onClose: this.handleClose})
setTimeout(() => {
this.$Toast.hide()
}, 3000)
},
handleClose () {
console.log('loading toast消失自定义回调函数')
}
}
}
</script>

API

属性 说明 类型 默认值
type 提示类型,可选值:[info, success, fail, offline, loading, custom],必选 String --
content 提示内容 String --
duration 自动关闭的延时,单位秒,配置为 null 则不自动关闭 Number 1.5
mask 是否显示透明蒙层,防止触摸穿透 Boolean true
position Toast显示位置,可选值 [center, top, bottom] String center
icon 自定义图标的链接地址,只在 typecustom 时生效 String ``
onClose 关闭后的执行函数 Function {}

Methods

事件名称 说明 参数
hide 手动关闭Toast --
  1. 1. Toast 轻提示 APP 该方法或组件支持移动端小程序调用 PC 该方法或组件支持桌面端小程序调用
    1. 1.1. 规则
    2. 1.2. 引入方式
    3. 1.3. 代码演示
    4. 1.4. API
    5. 1.5. Methods