making multiple http call after Switching between components

67 Views Asked by At

I have one query which is bit complex for me to explain but do ask me if you need any further details...

Angular version: 8. Subject : issue while Switching components is circular.

Like from Component A. -> B -> A -> B

Brief details of my problem

From 'A' component I have selected couple of employees then click on the apply filter button so that I can switch to B component.

On every employees check-box clicked emitting event using service so that from 'B' component I should be able to fetch those selected employees and do further logic (like. API call)

Switching from A to B is working as expected because based on selected employees I am hitting the API to get their details.

But to reset the selected employees I am redirecting back to A component so that I can add more or remove employees...

Now the problem what I was facing is

As I have logic in component B, to hit the API and get the employees details.

The problem is after one round back from component 'B' on every selection one hit goes to API to get updated emp details.

I know it's happening bcz of EvenEmitter but what is the best possible solution for this so that on every event emit it should not make API call from 'A' untill and untill I am not in component B.

1

There are 1 best solutions below

0
vijay sahu On

I have fixed my problem.. every time when we redirect to other route it will automatically call the ngOnDestroy where you can unsubscribe all the services that you have subscribed.

Syntax to unsubscribe:

Declare one property

ServiceSubscriptions: Subscription;

ngOnDestroy() {
    this.ServiceSubscriptions.unsubscribe();
    debugger;
}

Feel free to ask me if anybody has any query.