In this article we review how to use BehaviorSubject in Angular 12 : and also the differences between Observable VS Subject VS BehaviorSubject.
Using BehaviorSubject in Angular 12 : differences between Observable VS Subject VS BehaviorSubject
For this sake, BehaviorSubject needs a DEFAULT value upon creation, as it must always return a value on SUBSCRIPTION TIME , even if it hasn't received a "NEXT" operation call.
The difference between an Observable and a Subject is that it is an observer in addition to being an observable : you can also edit and emit and send values from a Subject in addition to subscribing to it.
Behavior subject can be turned into observable using the asObservable() method on BehaviorSubject.
Following is an example of using BehaviorSubject: here is the app to try
by Carmel Schvartzman
כתב: כרמל שוורצמן