0

When close a tab or browser, you need to send the data in the form to Vue. It is made through mounted or created and window.addEventListener("beforeunload", this.unload). Sending failed. When I tried to check the availability of the function, I got "undefined"

mounted() {
    window.addEventListener("beforeunload", this.unload);
}
methods: {
    unload () {
      console.log("START UNLOAD FUNC");
      console.log('start sync');
      this.test;
      console.log('finish sync');
    },
    test() {
      console.log("START TEST");
    },
}
I get the following in the log:
[1006/114208.084:INFO:CONSOLE(1)] "START UNLOAD FUNC"
[1006/114208.084:INFO:CONSOLE(1)] "start sync"
[1006/114208.084:INFO:CONSOLE(1)] "finish sync"

How can data be sent when the browser is closed?

Sergey Ka
  • 25
  • 9

1 Answers1

0
let body = this.body;
      
let headers = {
        type: 'application/json'
};
let blob = new Blob([JSON.stringify(body)], headers);
navigator.sendBeacon('http://127.0.0.1:8000/form/', blob);

This work for me

Sergey Ka
  • 25
  • 9