Cancel / Kill http api call angular 2

Sometimes httpan api call takes a long time to load data. In this case, if we switch to another component, it still continues to execute (we can see it in the browser console). So, is there a way we can cancel or kill the httpapi call when we switch to another component?

+4
source share
2 answers

You can “kill” it using the method unsubscribe()in the lifecycle event OnDestroy, assuming you are using subscriptions, for example:

mySubscription: any;

ngOnInit() {
    this.mySubscription = this.myHttpCall().subscribe...
}

ngOnDestroy() {
    this.mySubscription.unsubscribe();
}
+3
source

Source: https://habr.com/ru/post/1667131/


All Articles