vue,页面返回触发事件

// 禁用浏览器返回键

  history.pushState(null, null, document.URL);

  window.addEventListener('popstate', this.disableBrowserBack);

在mounted中添加以上代码,其中disableBrowserBack为方法,

在methods中定义方法

disableBrowserBack(){  
      alert("无法返回,请提交")
      history.pushState(null, null, document.URL);
      window.addEventListener('popstate', this.disableBrowserBack);
    }

如果在以上方法中不再加

history.pushState(null, null, document.URL);
window.addEventListener('popstate', this.disableBrowserBack);
则页面提示一次后再点击返回则依然会返回,如果添加以上代码,多次点击返回也可以
posted @ 2021-10-13 10:31  埃菲尔上的加菲猫  阅读(1144)  评论(0编辑  收藏  举报