vue中监听页面,禁止点击浏览器返回按钮返回

问题

某个页面限制用户点击浏览器返回按钮返回

解决方法

在该页面的vue实例中添加mounted destroyed钩子,并且添加goBack方法加以限制

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://www.jianshu.com/p/9c8da115ba2e