I want to save changes just before the user close the window, but it don't works when I quit the tab. I'm using router's canDeactivate method and it works when I refresh my page with F5, but when I close the tab datas are not saved. Here is my canDeactivate function in my component :
@HostListener('window:beforeunload')
canDeactivate(): Observable<boolean> | boolean {
let subject = new Subject<boolean>();
let observable = subject.asObservable();
this.service.save(this.data).then(() => {
subject.next(true);
});
return observable.first();
}
I already tried this too :
@HostListener('window:beforeunload')
canDeactivate(): Observable<boolean> | boolean {
let xmlhttp = new XMLHttpRequest(); // new HttpRequest instance
xmlhttp.withCredentials = false;
xmlhttp.open("PUT", "http://localhost:8080/rest/api");
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.send(this.data);
return false;
}