I am passing data from one component to another, but when I leave the component the subscription should get unsubscribed but it is not.
service:
sendDueDatetoPayments = new BehaviorSubject<any>(null);
communicateDueDatetoPayments(msg) {
this.sendDueDatetoPayments.next(msg);
}
sending component:
dialogRef.afterClosed().subscribe(confirm => {
if (confirm) {
this.sharedService.communicateDueDatetoPayments(this.dueDateUpdatedTransctions); //passing data
}
});
Receiving component:
dueDateSubscription: any;
ngOnInit(){
this.listenDueDateRes();
}
listenDueDateRes() {
this.dueDateSubscription = this.sharedService.sendDueDatetoPayments.subscribe((res: any) => {
console.log(res)
if (res) {
//do something after getting the response
}
});
}
ngOnDestroy(){
this.dueDateSubscription.unsubscribe();
}