关于uniapp:uniapp返回上一级页面前先判断是否需要弹出提示弹出提示后用户点击确定返回上一级页面点击取消就阻止返回

7次阅读

共计 590 个字符,预计需要花费 2 分钟才能阅读完成。

一、h5、app 用同一个办法,小程序要用另外的 (因为小程序用 onBackPress 监听不了)。
二、h5、app(此办法写在 methods 里):

        onBackPress(options) { //h5、app 拦挡返回
            if (this.show) {//this.show 为 true 才弹出提醒
                uni.showModal({
                    title: '提醒',
                    content: '答案未保留,确定退出吗',
                    success: function(res) {if (res.confirm) {
                            uni.reLaunch({url: "/pages/index/index"// 返回上一级页面})
                        } else if (res.cancel) {}}
                });
                return true
            }
        },

三、小程序(computed 跟 methods 同级,此办法写在 methods 下面):

    computed:{hasReplyC(){// 小程序拦挡返回 
            // #ifdef MP-WEIXIN
            if(!this.show){wx.disableAlertBeforeUnload()//this.show 为 false 则不须要弹出
            }else{
                wx.enableAlertBeforeUnload({// 提醒
                    message: "答案未保留,确定退出吗",
                    success: function(res) { },
                    fail: function(errMsg) {},})
            }
            // #endif
        }
    },

正文完
 0