H5页面返回时提示用户返回将丢失信息

效果图
在这里插入图片描述
代码,组件封装弹窗

<template>
 <div>
    <van-popup v-model="show" :close-on-click-overlay="false" class="popup">
        <div class="content">
            <div class="title">提示:</div>
            <div class="text">
                返回后当前数据将丢失,是否确认
            </div>
        </div>
        <div class="btns">
            <van-button class="btn" @click="cancel">取消</van-button>
            <van-button type="info" @click="sureBtn" class="btn">确认</van-button>
        </div>
    </van-popup>
 </div>
</template>

<script>
export default {
    components: {

    },
    props: {
        show: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
        }
    },
    computed: {

    },
    created() {

    },
    mounted() {

    },
    watch: {

    },
    methods: {
        cancel() {
            this.$emit('cancel')
        },
        sureBtn() {
            this.$emit('sureBtn')
        }
    }
}
</script>
<style scoped lang="scss">
.popup{
    width: 400px;
    border-radius: 15px;
    .content{
        padding: 24px;
        .title{
            margin-bottom: 12px;
            font-size: 32px;
        }
        .text{
            padding: 12px;
            font-size: 28px;
        }
    }
    .btns{
        margin-top: 24px;
        width: 100%;
        display: flex;
        .btn{
            flex: 1;
        }
    }
}
</style>

使用

<backPopup
      :show="isShow"
      @cancel="cancel"
      @sureBtn="sureBtn"
/>

<script>
import backPopup from '@/components/backPopup/index.vue'
export default {
    components: {
        backPopup
    },
    props: {

    },
    data() {
        return {
            isShow: false
        }
    },
    computed: {

    },
    created() {
    },
    mounted() {
        if (window.history && window.history.pushState) {
            // 向历史记录中插入了当前页
            history.pushState(null, null, location.href)
            window.addEventListener('popstate', this.goBack, false)
        }
    },
    destroyed() {
        window.removeEventListener('popstate', this.goBack, false)
    },
    watch: {

    },
    methods: {
        goBack() {
            this.isShow = true
            // if (this.isPublicize) { // 禁止浏览器返回
            //     history.pushState(null, null, location.href)
            //     this.isPublicize = false
            // } else { // 可以返回的时候
            //     this.sureBtn()
            //     // this.$router.go(-1)
            // }
        },
        sureBtn() {
            this.$router.go(-1)
        },
        cancel() {
            this.isShow = false
            history.pushState(null, null, location.href)
            this.isPublicize = true
        }
    }
}
</script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值