title: swan.showToast header: develop nav: api sidebar: toast_swan-showToast

webUrl: https://qft12m.smartapps.cn/swan-api/toast/toast

解释:显示消息提示框

方法参数

Object object

object参数说明

属性名 类型 必填 默认值 说明
title String 提示的内容
icon String success 图标,有效值"success"、"loading"、"none"
image String 自定义图标的本地路径,image 的优先级高于 icon
duration Number 2000 提示的延迟时间,单位毫秒。
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
mask Boolean false 是否显示透明蒙层,防止触摸穿透。

icon有效值

有效值 说明
success 显示成功图标,此时 title 文本最多显示 7 个汉字长度。默认值
loading 显示加载图标,此时 title 文本最多显示 7 个汉字长度。
none 不显示图标,此时 title 文本最多可显示两行。

示例

在开发者工具中预览效果

扫码体验

webUrl: https://qft12m.smartapps.cn/swan-api/toast/toast - 图1 请使用百度APP扫码

代码示例1 - 默认样式

:::codeTab

  1. <view class="card-area">
  2. <view class="top-description border-bottom">默认样式</view>
  3. <button bindtap="showToast" type="primary" hover-stop-propagation="true">默认toast</button>
  4. </view>
  1. Page({
  2. showToast() {
  3. swan.showToast({
  4. title: '默认toast',
  5. mask: false,
  6. success: res => {
  7. this.setData({'disabled': false});
  8. },
  9. fail: err => {
  10. console.log('showToast fail', err);
  11. }
  12. });
  13. }
  14. });

代码示例2 - 无图标toast

:::codeTab

  1. <view class="card-area">
  2. <view class="top-description border-bottom">
  3. <view>设置不显示图标</view>
  4. <view>icon: 'none'</view>
  5. </view>
  6. <button bindtap="showToastIcon" type="primary" hover-stop-propagation="true">无图标toast</button>
  7. </view>
  1. Page({
  2. showToastIcon() {
  3. swan.showToast({
  4. title: '单行最多十四个文字单行最多十四个文字',
  5. icon: 'none',
  6. mask: false,
  7. success: res => {
  8. this.setData({'disabled': false});
  9. },
  10. fail: err => {
  11. console.log('showToast fail', err);
  12. }
  13. });
  14. }
  15. });

代码示例3 - 显示loading图标

:::codeTab

  1. <view class="card-area">
  2. <view class="top-description border-bottom">
  3. <view>设置显示loading图标</view>
  4. <view>icon: 'loading'</view>
  5. </view>
  6. <button bindtap="showToastLoading" type="primary" hover-stop-propagation="true">loading toast</button>
  7. </view>
  1. Page({
  2. showToastLoading() {
  3. swan.showToast({
  4. mask: true,
  5. title: '正在加载...',
  6. icon: 'loading',
  7. mask: false,
  8. success: res => {
  9. this.setData({'disabled': false});
  10. },
  11. fail: err => {
  12. console.log('showToast fail', err);
  13. }
  14. });
  15. }
  16. });

代码示例4 - 延迟5000毫秒的toast

:::codeTab

  1. <view class="card-area">
  2. <view class="top-description border-bottom">
  3. <view>设置延迟时间</view>
  4. <view>duration: 5000</view>
  5. </view>
  6. <button bindtap="showToastDuration" type="primary" hover-stop-propagation="true">延迟5000毫秒的toast</button>
  7. </view>
  1. Page({
  2. showToastDuration() {
  3. swan.showToast({
  4. duration: 5000,
  5. title: '5000毫秒后隐藏',
  6. icon: 'none',
  7. mask: false,
  8. success: res => {
  9. this.setData({'disabled': false});
  10. },
  11. fail: err => {
  12. console.log('showToast fail', err);
  13. }
  14. });
  15. }
  16. });

代码示例5 - 隐藏toast

:::codeTab

  1. <view class="card-area">
  2. <view class="top-description border-bottom">隐藏toast</view>
  3. <button bindtap="hideToast" type="primary" disabled="{{disabled}}" hover-stop-propagation="true">隐藏toast</button>
  4. </view>
  1. Page({
  2. hideToast() {
  3. swan.hideToast();
  4. swan.hideLoading();
  5. }
  6. });

参考示例

:::

参考示例1 - 开发者可自定义showToast样式

在开发者工具中预览效果

:::codeTab

  1. <view class="wrap">
  2. <button type="primary" bindtap="clickbtn"> 点击 </button>
  3. <view animation="{{animationData}}" class="toast-view" s-if="{{showModalStatus}}">要显示的内容
  4. </view>
  5. </view>
  1. Page({
  2. data: {
  3. animationData: "",
  4. showModalStatus: false
  5. },
  6. showModal() {
  7. var animation = swan.createAnimation({
  8. duration: 200,
  9. timingFunction: "linear",
  10. delay: 0
  11. })
  12. animation.translateY(200).step()
  13. this.setData({
  14. animationData: animation.export(),
  15. showModalStatus: true
  16. })
  17. let that = this;
  18. setTimeout(function () {
  19. animation.translateY(0).step()
  20. that.setData({
  21. animationData: animation.export()
  22. })
  23. }, 200)
  24. setTimeout(function () {
  25. if(that.data.showModalStatus){
  26. that.hideModal();
  27. }
  28. }, 5000)
  29. },
  30. clickbtn() {
  31. if(this.data.showModalStatus){
  32. this.hideModal();
  33. }
  34. else {
  35. this.showModal();
  36. }
  37. },
  38. hideModal() {
  39. this.setData({
  40. showModalStatus: false
  41. })
  42. },
  43. })

:::

参考示例2 - showModal和showToast是否可共存

在开发者工具中预览效果

:::codeTab

  1. <view class="container">
  2. <view>
  3. <view class="card-area">
  4. <view class="top-description border-bottom">showModal和showToast可共存</view>
  5. <button data-title="success" data-icon="success" bindtap="showToast" type="primary" hover-stop-propagation="true">点击弹出toast</button>
  6. <button data-title="loading" data-icon="loading" bindtap="showModal" type="primary" hover-stop-propagation="true">点击弹出modal</button>
  7. </view>
  8. </view>
  9. </view>
  1. Page({
  2. data: { },
  3. showToast(e) {
  4. this.toast(e.currentTarget.dataset.title, e.currentTarget.dataset.icon);
  5. },
  6. toast(title, icon) {
  7. swan.showToast({
  8. title,
  9. icon,
  10. mask: false, // 此属性设置为true不能打断toast
  11. success: res => {
  12. console.log('showToast success', res);
  13. },
  14. fail: err => {
  15. console.log('showToast fail', err);
  16. }
  17. })
  18. },
  19. showModal(){
  20. swan.showModal({
  21. title: 'title',
  22. content: 'content'
  23. });
  24. }
  25. });

:::

错误码

Android

错误码 说明
202 解析失败,请检查参数是否正确
302 找不到调起协议对应端能力方法

iOS

错误码 说明
202 解析失败,请检查参数是否正确

Bug&Tip