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

一、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
        }
    },

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理