真·showModal

发布于 4 年前作者 fsun2109 次浏览最后编辑 4 年前来自 share

小程序自带的showModal并不能阻止线程的执行。例如:

      wx.showModal({
        title: title || "提示",
        content: text || "",
        showCancel: showCancel || true,
        successres => {
  	                   //这里的要等到操作以后才执行
          }
        })
//这里的不管有没有弹出,都会执行。

但是很多时候我们不想考虑那么多的回调,调过去调过来的头都大了。所以很想有一个能阻止线程的showModal,于是乎自己弄了一个。代码如下:

  /**
   * 阻挡线程的模态窗口,必须用await调用
   * @param {String} title 标题
   * @param {String} text 文本内容
   * @param {Boolean} showCancel 是否显示取消按钮,默认为显示
   */
async function showModal function (title, text, showCancel{
    return await new Promise(function (resolve, reject{
      wx.showModal({
        title: title || "提示",
        content: text || "",
        showCancel: showCancel || true,
        successres => {
          resolve(res);
        }
      })
    })
  }
});

可以挂接到getApp里面,全局调用。

调用方式:

var app = getApp();
// 前面的
let re = await app.showModal("标题","内容",false);
// 可以通过读取re的值来判断点的哪个按钮
1 回复
renjuan
renjuan1 楼2 个月前

await wx.showModal()

了解一下。