I have a BehaviorSubject
that is being consumed as an observable:
testForStack$: Observable<boolean>;
ngOnInit(){
const bs = new BehaviorSubject(true);
this.testForStack$ = bs
.asObservable()
.do(t => console.log('subscribed'))
.share();
}
This observable is being piped through three async
pipes in the template:
Sub1: {{testForStack$ | async}}<br>
Sub2: {{testForStack$ | async}}<br>
Sub3: {{testForStack$ | async}}
The issue is only the first (Sub1
) is getting the value of true
Sub1: true
Sub2:
Sub3:
If I remove the .share()
, all three values get the value of true, but this causes the issue of multiple subscriptions.
Any thoughts on why using the BehaviorSubject causes this behavior? It's being used as an observable so I would assume the above code would work correctly.
This is similar to this answer as well.