1.Usage scenarios
When there is a form that needs to be filled in by the user on the page, we need to consider whether the user is careless Problem with the return page. This will cause all the data entered by the user to be destroyed, and the user experience will be extremely poor. We don't need to use complicated methods to monitor page rollbacks.
Officially provides API wx.enableAlertBeforeUnload()
Pop-up window conditions
- When the user is on a non-home page/bottom page in the mini program
- Return on the official navigation bar
- Self-drawn return key in full screen mode
- android system back key
Precautions
- No interception when gesture sliding returns
- In any scenario, this function should not prevent users from exiting the mini program.
Reference Code
onLoad: function(){
wx.enableAlertBeforeUnload({
message: "您确定要退出预约吗?",
success: function (res) {
console.log("方法注册成功:", res);
},
fail: function (errMsg) {
console.log("方法注册失败:", errMsg);
},
});
}