关闭选项卡或浏览器时,需要将表单中的数据发送给Vue。它是通过挂载或创建和window.addEventListener("beforeunload", this.unload)
制作的。发送失败了。当我试图检查函数的可用性时,我得到了"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"
浏览器关闭时如何发送数据?
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