調(diào)試的時候發(fā)現(xiàn)toast閃過的太快了,而且在模擬器上是沒有問題的,在真機上就不行
后來給toast增加時長也不管用慧耍,了解了之后總結(jié)一下問題
toast閃過的快有兩種情況
- 一種是加載的時候先是showloading,加載完成之后彈toast提示一下成功或者失敗,這個toast關(guān)閉的特別快昌腰,后來發(fā)現(xiàn)showloading和hideloading必須得是搭配使用的,就是說如果showloading之后緊跟著showtoast的話是不行的膀跌,所以
在showtoast之前一定要先關(guān)閉loading
//打開loading
wx.showLoading({
title: '加載中...',
mask: true,
});
wx.request({
url:url,
data: {
...
},
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST",
dataType: "json",
success(res) {
//關(guān)閉loading
wx.hideLoading()
//打開toast
wx.showToast({
title: res.data.msg,
icon: 'none',
duration: 3000
});
},
fail() {
//關(guān)閉loading
wx.hideLoading()
//打開toast
wx.showToast({
title: '發(fā)送失敗',
icon: 'none',
duration: 3000
});
}
})
要先關(guān)閉loading再打開toast遭商,就不會一閃而過了
- 還有一種情況就是,提示成功之后跳轉(zhuǎn)頁面捅伤,在A頁面提示之后跳轉(zhuǎn)到B頁面劫流,toast不會跟隨著展示到B頁面,toast在跳轉(zhuǎn)的時候就關(guān)閉了丛忆,所以
把跳轉(zhuǎn)放在toast執(zhí)行之后再執(zhí)行
wx.showToast({
title: '綁定成功',
icon: 'success',
duration: 3000,
success() {
//計時器
setTimeout(function () {
wx.switchTab({
url: '/pages/mine/mine',
fail: (e) => {
console.log(e)
wx.showToast({
title: e.errMsg || err.msg || e,
icon: 'none',
duration: 3000
});
}
});
}, 3000)
}
});
加一個計時器祠汇,等toast執(zhí)行完了再跳轉(zhuǎn),toast就能正常展示了