记录: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():

你可能感兴趣的:(记录:uni-app 监听浏览器返回事件,onBackPress生命周期失效问题)