效果:
wx.enableAlertBeforeUnload:开启监听;
wx.disableAlertBeforeUnload:取消监听;
应用场景:在填写地址栏页面,我们需要做退出页面拦截
验证表单
//表单失去焦点核验表单
ValidationForm() {
if (this.form.name || this.form.phone || this.form.address || this.form.areaId) {
this.isFormChange = true;
} else {
this.isFormChange = false;
//取消监听
wx.disableAlertBeforeUnload();
}
},
watch: {
isFormChange(val) {
if (val) {
wx.enableAlertBeforeUnload({
message: '当前信息未保存,退出将会丢失,是否退出?'
});
}
}
},
当保存完信息后记得要取消监听