RxJS - 发布行为操作符 publishBehavior
publishBehaviour 使用 BehaviourSubject,并返回 ConnectableObservable。connect() 方法必须用于订阅创建的可观察对象。
语法
publishBehaviour(defaultvalue)
示例
import { interval} from 'rxjs'; import { take, publishBehavior} from 'rxjs/operators'; let observer = interval(1000).pipe( take(5), publishBehavior(4) ); const subscribe_one = observer.subscribe( x => console.log("Value from Sub1 = "+x) ); const subscribe_two = observer.subscribe( x => console.log("Value from Sub2 = "+x) ); observer.connect(); console.log("After 2 seconds"); setTimeout(() => { const subscribe_three = observer.subscribe( x => console.log("Value from Sub3 = "+x) ); }, 2000);
输出
首先会显示默认值,然后显示来自可观察对象的值。
广告