navigator.sendBeacon方法可以搜索文档具体用法,它支持的传参形式是有限制的。

在APP.vue中写页面关闭时触发的方法,

mounted(){
  //页面打开注册关闭事件
  //unload
  //onbeforeunload
  let url = xxx/xxx/xxx
  window.addEventListener("unload", (event) => {
      navigator.sendBeacon(url , JSON.stringify({
        data:  {
          text: 123
        }
      }));
      window.localStorage.setItem("stored", "1");
      // Cancel the event as stated by the standard.
      event.preventDefault();
      // Chrome requires returnValue to be set.
      event.returnValue = "true";
    });
 },

  

 

posted @ 2021-03-12 17:21  sinceForever  阅读(285)  评论(0编辑  收藏  举报