美文网首页
VUE中监听浏览器返回

VUE中监听浏览器返回

作者: 90后徐先生 | 来源:发表于2020-01-16 16:04 被阅读0次
    因为我的项目不是嵌套在APP中的,所以出现了在APP中浏览器默认带前后返回键的情况。 所以在App.vue中添加了这个方法后,生效
    mounted () {
        if (window.history && window.history.pushState) {
            // 向历史记录中插入了当前页
            history.pushState(null, null, document.URL);
            window.addEventListener('popstate', this.goBack, false);
        }
    },
      destroyed () {
        window.removeEventListener('popstate', this.goBack, false);
      },
    methods: {
        goBack () {
            // console.log("点击了浏览器的返回按钮");
            history.pushState(null, null, document.URL);
        },
    } 
    

    其余相关资料 https://blog.csdn.net/qq_32462903/article/details/90084887

    相关文章

      网友评论

          本文标题:VUE中监听浏览器返回

          本文链接:https://www.haomeiwen.com/subject/migfzctx.html