uni-app 小程序页面返回提示无法取消

uni-app 小程序页面返回提示无法取消

开发环境 版本号 项目创建方式
Windows win10 HBuilderX
HBuilderX 3.1.11
Stable 1.05.2103200
2.16.1

示例代码:

//发起请求获取数据后开启提示
wx.enableAlertBeforeUnload({
message: "当前测评未答完,确认退出?",
complete: (e) => {
console.log(e);
}
})  

//**
//**
//页面离开时判断是否完成
onUnload() {
var that = this
if (that.submitState) {
wx.disableAlertBeforeUnload({
complete: (e) => {
console.log(e);
//这里返回disableAlertBeforeUnload:ok 但是还是有弹窗提示
}
})
}
},

操作步骤:

  • 进入获取数据后开启返回提示

预期结果:

  • 用户未答完就提示弹窗,答完就关闭错误

实际结果:

  • 用户答完或未答完都提示返回弹窗

bug描述:

wx.enableAlertBeforeUnload和wx.disableAlertBeforeUnload都返回ok状态,但是只有返回提示生效了,取消弹窗功能无效

更多关于uni-app 小程序页面返回提示无法取消的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于uni-app 小程序页面返回提示无法取消的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这是一个常见的小程序API使用问题。wx.disableAlertBeforeUnload 需要在页面卸载前调用,但在 onUnload 生命周期中调用已经太晚了。

问题分析:

  1. onUnload 触发时页面即将销毁,此时禁用返回提示为时已晚
  2. 应该在提交完成时立即调用 wx.disableAlertBeforeUnload
  3. 弹窗触发时机在页面卸载之前,而 onUnload 在卸载时执行

解决方案:

// 提交完成后立即禁用返回提示
submitForm() {
  // 提交逻辑...
  this.submitState = true;
  
  // 立即禁用返回提示
  wx.disableAlertBeforeUnload({
    complete: (e) => {
      console.log('禁用返回提示:', e);
    }
  });
},

onUnload() {
  // 这里不再需要处理禁用逻辑
}
回到顶部