Within the initialization of my component, I have the following code:
public Subscription: Subscription;
ngOnInit() {
this.subscription = this.myService.currentData.subscribe( dataReceived => {
this.data = dataReceived;
this.useData(this.data);
});
}
Initially, subscribing to the data in the service works perfectly. However, after navigating to another module and returning, the function gets triggered twice. With each repetition of this process, the function is executed multiple times. For example, if I switch modules back and forth five times, the function will be called five times.
I attempted to fix this issue by adding an unsubscribe method in ngOnDestroy
:
ngOnDestroy() {
this.subscription.unsubscribe();
}
Unfortunately, this solution does not resolve the problem as the multiple triggers still persist.