toast显示消息提示

本文编辑: DevelopeWhite DevelopeWhite浏览 5840 版权所有,严禁转载

组件说明:

Toast是当用户点击某些组件时弹出来来的提示消息。Toast会帮助你创建和显示这些消息。Toast是一种简易的消息提示框。当视图显示给用户,在应用程序中显示为浮动。小程序即将废弃Toast组件,故这里介绍Toast的API:wx.showToast

组件用法:


wxml

<!--index.wxml-->
<view class="content">
  <text class="showfunc">Toast功能</text>
  <view class="con-button">
    <button class="button-left" bindtap="showToast">展示Toast</button>
    <button class="button-right" bindtap="hideToast">隐藏Toast</button>
  </view>
</view>

js

Page({
  showToast:function(){
    var that=this;
    wx.showToast({
      title: '成功',
      icon: 'success',
      duration: 2000,
      success: that.flySuccess,
      fail:that.flyFail,
      complete:that.flyComplete
    })
  },

  hideToast:function(){
    var that=this;
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000,
      success: that.loadingSuccess,
      fail:that.loadingFail,
      complete:that.loadingComplete
    });

    setTimeout(function(){
      wx.hideToast()
    },2000)
  },

  flySuccess:function(e){
    console.log(e);
    console.log("起飞成功!");
  },
  flyFail:function(e){
    console.log(e);
    console.log("起飞失败!")
  },
  flyComplete:function(e){
    console.log(e);
    console.log("起飞结束!")
  },
  loadingSuccess:function(e){
    console.log(e);
    console.log("加载成功!");
  },
  loadingFail:function(e){
    console.log(e);
    console.log("加载失败!")
  },
  loadingComplete:function(e){
    console.log(e);
    console.log("加载结束!")
  }
})

wxss

/**index.wxss**/
.con-button{
  display: flex;
  flex-direction: row;
  padding-top: 10%;
}

.showfunc{
  padding-top:10%;
  display: block;
  text-align: center;
  color: green;
}

主要参数:

参数 类型 是否必填 参数描述
title String 该提示框提示的内容
icon String 该提示框的图标,只支持”success”、”loading”
duration Number 提示的延迟时间,单位毫秒,默认:1500, 最大为10000
success Function 该接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
如有技术问题或对本文有反馈,请加入QQ群:
微信小程序实战5营: 微信小程序Club实战5营