4-2 微信小程序提示框、loading框

wx.showToast(Object object)

显示消息提示框

参数

Object object

属性类型默认值必填说明最低版本
titlestring 提示的内容 
iconstring'success'图标 
imagestring 自定义图标的本地路径,image 的优先级高于 icon1.1.0
durationnumber1500提示的延迟时间 
maskbooleanfalse是否显示透明蒙层,防止触摸穿透 
successfunction 接口调用成功的回调函数 
failfunction 接口调用失败的回调函数 
completefunction 接口调用结束的回调函数(调用成功、失败都会执行) 

object.icon 的合法值

说明最低版本
success显示成功图标,此时 title 文本最多显示 7 个汉字长度 
loading显示加载图标,此时 title 文本最多显示 7 个汉字长度 
none不显示图标,此时 title 文本最多可显示两行,1.9.0及以上版本支持 

示例代码

wx.showToast({
  title: '成功',
  icon: 'success',
  duration: 2000
})

 

注意

wx.hideToast(Object object)

隐藏消息提示框

参数

Object object

属性类型默认值必填说明
successfunction 接口调用成功的回调函数
failfunction 接口调用失败的回调函数
completefunction 接口调用结束的回调函数(调用成功、失败都会执行)

 

wx.showModal(Object object)

显示模态对话框

参数

Object object

属性类型默认值必填说明
titlestring 提示的标题
contentstring 提示的内容
showCancelbooleantrue是否显示取消按钮
cancelTextstring'取消'取消按钮的文字,最多 4 个字符
cancelColorstring#000000取消按钮的文字颜色,必须是 16 进制格式的颜色字符串
confirmTextstring'确定'确认按钮的文字,最多 4 个字符
confirmColorstring#576B95确认按钮的文字颜色,必须是 16 进制格式的颜色字符串
successfunction 接口调用成功的回调函数
failfunction 接口调用失败的回调函数
completefunction 接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数

Object res

属性类型说明最低版本
confirmboolean为 true 时,表示用户点击了确定按钮 
cancelboolean为 true 时,表示用户点击了取消(用于 Android 系统区分点击蒙层关闭还是点击取消按钮关闭)1.1.0

示例代码

wx.showModal({
  title: '提示',
  content: '这是一个模态弹窗',
  success(res) {
    if (res.confirm) {
      console.log('用户点击确定')
    } else if (res.cancel) {
      console.log('用户点击取消')
    }
  }
})

 

注意

  • Android 6.7.2 以下版本,点击取消或蒙层时,回调 fail, errMsg 为 "fail cancel";
  • Android 6.7.2 及以上版本 和 iOS 点击蒙层不会关闭模态弹窗,所以尽量避免使用「取消」分支中实现业务逻辑

wx.showLoading(Object object)

基础库 1.1.0 开始支持,低版本需做兼容处理

显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框

参数

Object object

属性类型默认值必填说明
titlestring 提示的内容
maskbooleanfalse是否显示透明蒙层,防止触摸穿透
successfunction 接口调用成功的回调函数
failfunction 接口调用失败的回调函数
completefunction 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码

wx.showLoading({
  title: '加载中',
})

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

 

注意

wx.hideLoading(Object object)

基础库 1.1.0 开始支持,低版本需做兼容处理

隐藏 loading 提示框

参数

Object object

属性类型默认值必填说明
successfunction 接口调用成功的回调函数
failfunction 接口调用失败的回调函数
completefunction 接口调用结束的回调函数(调用成功、失败都会执行)

 

另外在wxml中也可以使用loading组件来显示加载,在data中设置一个变量来控制显示隐藏即可

<loading hidden="{{hidden}}">
    加载中...
</loading>

 

wx.showActionSheet(Object object)

显示操作菜单

参数

Object object

属性类型默认值必填说明
itemListArray.<string> 按钮的文字数组,数组长度最大为 6
itemColorstring#000000按钮的文字颜色
successfunction 接口调用成功的回调函数
failfunction 接口调用失败的回调函数
completefunction 接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数

Object res

属性类型说明
tapIndexnumber用户点击的按钮序号,从上到下的顺序,从0开始

示例代码

wx.showActionSheet({
  itemList: ['A', 'B', 'C'],
  success(res) {
    console.log(res.tapIndex)
  },
  fail(res) {
    console.log(res.errMsg)
  }
})

 

注意

  • Android 6.7.2 以下版本,点击取消或蒙层时,回调 fail, errMsg 为 "fail cancel";
  • Android 6.7.2 及以上版本 和 iOS 点击蒙层不会关闭模态弹窗,所以尽量避免使用「取消」分支中实现业务逻辑

以上文本内容来自文档:https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值