记录:uni-app 监听浏览器返回事件,onBackPress生命周期失效问题

解决办法:

onLoad() {

          if (window.history && window.history.pushState) {

                   history.pushState(null, null, document.URL);

                    window.addEventListener("popstate", this.show11, false);

          }

},

onUnload() {

          window.removeEventListener("popstate", this.show11, false);

},

methods: {

          show11() {

                  uni.showModal({

                              content: '保留此次编辑?',

                              cancelText: '不保留',

                              confirmText: '保留',

                              success: function (res) {

                                        if (res.confirm) {

                                                  uni.showToast({

                                                            title: '用户点击保留',

                                                            duration: 1000

                                                    })

                                        } else if (res.cancel) {

                                                  uni.showToast({

                                                            title: '用户点击不保留',

                                                            duration: 1000

                                                    })

                                        }

                              }

                    })

          }

}

浏览器默认返回按键及Android手机实体返回键不支持 onBackPress():

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容