Currently, I am working on an Angular 4 application that consists of 5 components. My goal is to trigger an API call when the user closes the browser window from any one of these components.
However, I have encountered an issue where the API does not get called when I close the application.
In my app.component.ts file, I have the following code:
export class AppComponent implements OnInit {
constructor(private CartdataService: CartdataService, private http: HttpClient) { }
ngOnInit() {}
@HostListener('window:beforeunload', ['$event'])
beforeunloadHandler(event) {
this.WindowClosed();
}
WindowClosed() {
this.CartdataService.get_DummyCall().subscribe();
}
}
As of now, I have only implemented the above code and I am unsure of what mistakes I may have made. Can anyone provide insight into where I might be going wrong?
For additional reference, you can visit the following link: Angular 2 - Execute code when closing window