当wx.showLoading 和 wx.showToast 混合使用时,showLoading和showToast会相互覆盖对方,调用hideLoading时也会将toast内容进行隐藏。
request封装情况下(Promise):
当我们给一个网络请求增加Loading态时,如果同时存在多个请求(A和B),如果A请求失败需要将错误信息以Toast形式展示,B请求完成后又调用了wx.hideLoading来结束Loading态,此时Toast也会立即消失,不符合展示一段时间后再隐藏的预期。
接口请求成功,hideLoading会同时把showToast隐藏掉
// 注意此代码应该在调用原生api之前执行
let isShowLoading = false;
let isShowToast = false;
const {
showLoading,
hideLoading,
showToast,
hideToast
} = wx;
Object.defineProperty(wx, 'showLoading', {
configurable: true, // 是否可以配置
enumerable: true, // 是否可迭代
writable: true, // 是否可重写
value(...param) {
if (isShowToast) { // Toast优先级更高
return;
}
isShowLoading = true;
console.log('--------showLoading--------')
return showLoading.apply(this, param); // 原样移交函数参数和this
}
});
Object.defineProperty(wx, 'hideLoading', {
configurable: true, // 是否可以配置
enumerable: true, // 是否可迭代
writable: true, // 是否可重写
value(...param) {
if (isShowToast) { // Toast优先级更高
return;
}
isShowLoading = false;
console.log('--------hideLoading--------')
return hideLoading.apply(this, param); // 原样移交函数参数和this
}
});
Object.defineProperty(wx, 'showToast', {
configurable: true, // 是否可以配置
enumerable: true, // 是否可迭代
writable: true, // 是否可重写
value(...param) {
if (isShowLoading) { // Toast优先级更高
wx.hideLoading();
}
isShowToast = true;
console.error('--------showToast--------')
return showToast.apply(this, param); // 原样移交函数参数和this
}
});
Object.defineProperty(wx, 'hideToast', {
configurable: true, // 是否可以配置
enumerable: true, // 是否可迭代
writable: true, // 是否可重写
value(...param) {
isShowToast = false;
console.error('--------hideToast--------')
return hideToast.apply(this, param); // 原样移交函数参数和this
}
});