vue 手机物理返回键关闭弹框

1、打开弹窗调用 window.history.pishState() 函数

Transport() {

      this.ship = true;

      window.history.pushState(null, null, "#");

    },


2、关闭弹框

guo_mask() {

      this.ship = false;

      this.$router.go(-1);

    },

3、mounted 生命周期 监听popstate 事件


 mounted() {

    window.addEventListener("popstate", e => {

        if (this.ship) {

          this.ship = !this.ship;

        }

      },false);

  },


 4、beforeDestroy 生命周期 移除popstate 事件

beforeDestroy() {

    window.removeEventListener("popstate",e => {

        if (this.ship) {

          this.ship = !this.ship;

        }

      },false);

  },



 个人觉得这种方法虽然实现了效果,但并不完美,比较繁琐。希望有好建议的大佬留言!



©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容