// RxJS v6+import { BehaviorSubject } from'rxjs';constsubject=newBehaviorSubject(123);// two new subscribers will get initial value => output: 123, 123subject.subscribe(console.log);subject.subscribe(console.log);// two subscribers will get new value => output: 456, 456subject.next(456);// new subscriber will get latest value (456) => output: 456subject.subscribe(console.log);// all three subscribers will get new value => output: 789, 789, 789subject.next(789);// output: 123, 123, 456, 456, 456, 789, 789, 789
Example 2: BehaviorSubject with new subscribers created on mouse clicks