I have a service with a behaviorSubject element, like this:
public _changeAlert = new BehaviorSubject<myMsg>(null);
public changeAlert = this._changeAlert.asObservable();
public sendAlert(msg$: string){
this._changeAlert.next(msg$);
}
And have a subscriber receiverComponent.ts
alerterPointer: any;
constructor(private alertSrv: alertService){
this.alerterPointer = this.alertSrv.changeAlert.subscribe(msg$ => {console.info(msg$)});
}
}
ngOnDestroy(){
this.alerterPointer.unsubscribe();
component that sends/broadcast alert alerter.ts
sendAlert(){
this.alertSrv.sendAlert('Hello world');
}
My issue is that, even after unsubscribing, if I naviate to another component and come back to the receiverComponent.ts, it actually gets the "hello world" instead of the null value of the initial subscription. In other words, it remembers the last alert broadcasted.
How do I avoid this leak ? ... I'm using this approach all over my project and have substantial leaks bc of this behavior.
(pls don't tag it as duplicated, I checked the similar question but it doesn't address the same issue)
thanks !