• 【小程序】展示弹窗常见API详解


    展示弹窗常见的API

    小程序中展示弹窗有四种方式: showToastshowModalshowLoadingshowActionSheet

    showToast

    显式消息提示框

    有如下一些属性:

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

    其中icon属性有如下一些值:

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

    演示代码

    wx.showToast({
      title: "购买失败",
      icon: "error",
      duration: 100,
      mask: true,
      success: (res) => {
        console.log("展示成功: ", res);
      },
      fail: (err) => {
        console.log("展示失败: ", err);
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    showModal

    显示模态对话框

    常见属性如下:

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

    演示代码

    wx.showModal({
      title: "四个二",
      cancelText: "要不起",
      cancelColor: '#f00',
      confirmText: "管上",
      confirmColor: "skyblue",
      success: (res) => {
        console.log("展示成功: ", res);
      },
      fail: (err) => {
        console.log("展示失败: ", err);
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在成功的回调函数中, 有如下属性判断用户点击了确定还是取消

    属性类型说明
    contentstringeditable 为 true 时,用户输入的文本
    confirmboolean为 true 时,表示用户点击了确定按钮
    cancelboolean为 true 时,表示用户点击了取消(用于 Android 系统区分点击蒙层关闭还是点击取消按钮关闭)
    wx.showModal({
      title: "四个二",
      cancelText: "取消",
      confirmText: "确定",
      success: (res) => {
        console.log("展示成功: ", res);
        if (res.cancel) {
          console.log("用户点击了左边取消按钮");
        } else if (res.confirm) {
          console.log("用户点击了右边确定按钮");
        }
      },
      fail: (err) => {
        console.log("展示失败: ", err);
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    showLoading

    显示 loading 提示框。与showToast的区别是, 守卫Loading需主动调用 wx.hideLoading 才能关闭提示框

    其中的属性如下:

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

    演示代码

    wx.showLoading({
    			title: '加载中',
    			mask: true,
    			success: (res) => {
    				console.log(res);
    			},
    			fail: (err) => {
    				console.log(err);
    			}
    		})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    showActionSheet

    显示操作菜单

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

    演示代码

    wx.showActionSheet({
      itemList: ["Macbook", "iPad", "iPhone"],
      itemColor: "#f00",
      success: (res) => {
        console.log(res);
      },
      fail: (err) => {
        console.log(err);
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    成功的回调res中的属性

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

    可以通过tapIndex知道点击了哪个按钮

    wx.showActionSheet({
      itemList: ["Macbook", "iPad", "iPhone"],
      itemColor: "#f00",
      success: (res) => {
        console.log(res .tapIndex);
      },
      fail: (err) => {
        console.log(err);
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    注意

    wx.showToastwx.showLoading同时只能显示一个

    wx.showLoadingwx.hideLoading配对使用

  • 相关阅读:
    基于python-socket的端口扫描
    使用WildCard充值ChatGPT Plus 会员
    Linux常用网络命令参数整理_incomplete
    【Python3】【力扣题】338. 比特位计数
    Docker部署Nacos2.0单机版+mysql8
    三体目标管理笔记
    通过xsd校验xml
    Mongodb安全访问控制操作案例
    5、Redis的发布和订阅
    PointNet: Deep Learning on Point Sets for 3D Classification and Segmentation
  • 原文地址:https://blog.csdn.net/m0_71485750/article/details/126433233