Subjects and BehaviorSubject
Multicast values with subjects.
What is a Subject
A Subject is both an Observable and an Observer. You can subscribe to it and also push values into it with next(). It multicasts: all subscribers receive the same emissions.
Creating and using a Subject
Subscribers added after a value is emitted will not receive past values.
import { Subject } from 'rxjs';
const events$ = new Subject<string>();
events$.subscribe(v => console.log('A:', v));
events$.next('hello'); // A: hello
events$.subscribe(v => console.log('B:', v));
events$.next('world'); // A: world, B: worldAll lessons in this course
- Observables and Subscriptions
- Subjects and BehaviorSubject
- Unsubscribing and Memory Leaks
- The async Pipe