一、h5、app用同一个办法,小程序要用另外的(因为小程序用onBackPress监听不了)。
二、h5、app(此办法写在methods里):
onBackPress(options) { //h5、app拦挡返回 if (this.show) {//this.show为true才弹出提醒 uni.showModal({ title: '提醒', content: '答案未保留,确定退出吗', success: function(res) { if (res.confirm) { uni.reLaunch({ url: "/pages/index/index"//返回上一级页面 }) } else if (res.cancel) {} } }); return true } },
三、小程序(computed跟methods同级,此办法写在methods下面):
computed:{ hasReplyC(){//小程序拦挡返回 // #ifdef MP-WEIXIN if(!this.show){ wx.disableAlertBeforeUnload()//this.show为false则不须要弹出 }else{ wx.enableAlertBeforeUnload({//提醒 message: "答案未保留,确定退出吗", success: function(res) { }, fail: function(errMsg) { }, }) } // #endif } },