页面刷新或离开页面给后端发送数据,Vue监听页面刷新或关闭

语法:navigator.sendBeacon(url, data);

例子(写在mounted中):

window.addEventListener("beforeunload", (e) => {
  window.navigator.sendBeacon(url, '测试发送的数据');
});

参考链接:https://developer.mozilla.org/zh-CN/docs/Web/API/Navigator/sendBeacon

 或者这种方式

window.onbeforeunload = e => {
   this.level_page()
  }

 

posted @ 2021-08-24 19:51  yw3692582  阅读(184)  评论(0编辑  收藏  举报