微信开发者工具界面交互方法

179 阅读3分钟

这是我参与11月更文挑战的第14天,活动详情查看:2021最后一次更文挑战

wx.showToast 显示消息提示框 🍈🍈🍈

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

我们可以先把方法封装起来,方便多次调用

Promise 对象代表了未来将要发生的事件,用来传递异步操作的消息。 有了 Promise 对象,就可以将异步操作以同步操作的流程表达出来,避免了层层嵌套的回调函数。此外,Promise 对象提供统一的接口,使得控制异步操作更加容易。

Promise 也有一些缺点。首先,无法取消 Promise,一旦新建它就会立即执行,无法中途取消。其次,如果不设置回调函数,Promise 内部抛出的错误,不会反应到外部。第三,当处于 Pending 状态时,无法得知目前进展到哪一个阶段(刚刚开始还是即将完成)。

Promise 构造函数包含一个参数和一个带有 resolve 解析reject 拒绝两个参数的回调。在回调中执行一些操作(例如异步),如果一切都正常,则调用 resolve,否则调用 reject。

// 需要传递两个参数 title, icon 来改变消息提示框的状态
export const showToast=({title, icon})=>{
    return new Promise((resolve, reject)=>{
      wx.showToast({
        title: title,
        icon: icon,
        duration: 3000,
        success: (res)=>{
          resolve(res)
        },
        fail:(err)=>{
          reject(err)
        }
      })
    })
}

导入封装文件中的方法和支持async的runtime.js

import {showToast} from "../../utils/asyncWx.js"
import regeneratorRuntime from "../../lib/runtime/runtime.js";

async getShowToast(){
    const res = await showToast({title: '删除成功', icon: "success"}) 
    console.log(res);
}

image.png

image.png

wx.showToast 应与 wx.hideToast 配对使用 执行 wx.hideToast(); 就会直接关闭了

wx.showModal 显示模态对话框 🍉🍉🍉

属性类型默认值必填说明
titlestring提示的标题
contentstring提示的内容
showCancelbooleantrue是否显示取消按钮
cancelTextstring取消取消按钮的文字,最多 4 个字符
cancelColorstring#000000取消按钮的文字颜色,必须是 16 进制格式的颜色字符串
confirmTextstring确定确认按钮的文字,最多 4 个字符
confirmColorstring#576B95确认按钮的文字颜色,必须是 16 进制格式的颜色字符串
editablebooleanfalse是否显示输入框
placeholderTextstring显示输入框时的提示文本
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
// 需要传递1个参数 content
export const showModal=({content})=>{
    return new Promise((resolve, reject)=>{
      wx.showModal({
        title: '提示',
        content: content,
        success: (res)=>{
          resolve(res)
        },
        fail:(err)=>{
          reject(err)
        }
      })
    })
}
async getsSowModal(){
    const res = await showModal({content: '确定删除吗?'}) 
    console.log(res);
}

image.png

image.png

返回的数据 cancel为true代表点击了取消按钮, confirm为true代表点击了确定按钮

image.png

wx.showLoading显示 loading 提示框 🍊🍊🍊

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

使用setTimeout方法,两秒后自动关闭

wx.showLoading({
    title: '加载中',
})
setTimeout(function () {
    wx.hideLoading()
}, 2000)

image.png

wx.showActionSheet 显示操作菜单 🍌🍌🍌

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

在页面下方弹出操作选项

export const getShowActionSheet=({itemList})=>{
  return new Promise((resolve, reject)=>{
    wx.showActionSheet({
      itemList: itemList,
      itemColor: "blue",
      success: (res)=>{
        resolve(res)
      },
      fail:(err)=>{
        reject(err)
      }
    })
  })
}
async getShowActionSheet(){
    const itemList =  ['苹果', '香蕉', '梨子'];
    const res = await getShowActionSheet({itemList: itemList}) 
    console.log(res);
}

image.png

点击香蕉的返回结果, tapIndex是选项的下标

image.png