返回时关闭弹窗而不是直接退出页面

分两种情况:

一种是直接进入到该页面,通过监听popstate事件来判断返回操作

1、在触发弹窗弹出的方法里加上:window.history.pushState(null, null, "#");

methods:{
   addPopState () {
        if (this.typeVisible) {//关闭弹窗
          this.typeClose = true
          this.closeModal()
        } else {//退出页面
          finishPage() 
        }  
    },  
}, 
mounted () {
    window.addEventListener("popstate", this.addPopState, false);
},
beforeUnmount () {
    window.removeEventListener("popstate", this.addPopState,false);
},      


一种是通过h5页面(路由)进入到该页面,此时可以通过beforeRouteLeave来判断返回操作

2、

  beforeRouteLeave (to, from, next) {
    console.log(to, from)
    if (this.typeVisible) {//关闭弹窗
      this.typeClose = true
      this.closeModal()
      next(false)
    } else {//返回到上一页
      next()
    }
  },

 

posted @ 2022-04-26 14:21  chicidol  阅读(179)  评论(0编辑  收藏  举报