微信小程序点击左上角返回弹窗提示解决思路
业务需求:当页面表单没有提交直接返回时,要提示用户是否保存当前信息,如果已经提交就不提示了。
由于微信小程序是无法监听右上角按钮返回事件。
所以就换个思路
小程序提供了如下两个api
wx.enablealertbeforeunload(object object):开启小程序页面返回询问对话框
wx.disablealertbeforeunload:关闭小程序页面返回询问对话框
实现方法如下:
onload: function (options) { this.enablefun() }, enablefun() { wx.enablealertbeforeunload({ message: '离开当前页面数据将会被清空', success(res) { console.log('success:', res) }, fail(res) { console.log('fail:', res) }, complete(res) { console.log('complete:', res) } }) }
如果在某种情况下(如已经提交了数据),不需要弹出提示框
onsubmit(){ this.disablefun() }, // 关闭小程序页面返回询问对话框 disablefun() { wx.disablealertbeforeunload({ success(res) { console.log('success:', res) }, fail(res) { console.log('fail:', res) }, complete(res) { console.log('complete:', res) } }) }
微信小程序阻止用户返回上一页,并弹窗给用户确定是否要返回上一页
在onload中调用微信的enablealertbeforeunload方法,在首次进入会自动监听当前的页面,在返回的时候会自动弹出弹窗阻止用户返回上一页,点击确定则返回上一页,取消则停留在当前页
onload: function(){ wx.enablealertbeforeunload({ message: "返回上页时弹出对话框1212", success: function (res) { console.log("方法注册成功:", res); }, fail: function (errmsg) { console.log("方法注册失败:", errmsg); }, }); }
到此这篇关于微信小程序 点击左上角返回弹窗提示的文章就介绍到这了,更多相关小程序点击返回弹窗提示内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论