RxJS - 发布多播运算符
publish() 运算符返回 ConnectableObservable,需要使用 connect() 方法来订阅该可观察对象。
语法
publish()
示例
import { interval,Observable } from 'rxjs'; import { filter, publish } from 'rxjs/operators'; var observable = new Observable(function subscribe(subscriber) { try { subscriber.next(Math.random()); } catch (e) { subscriber.error(e); } }); const observable1 = publish()(observable); const subscribe_one = observable1.subscribe( x => console.log("Value from Sub1 = "+x) ); const subscribe_two = observable1.subscribe( x => console.log("Value from Sub2 = "+x) ); observable1.connect();
输出
广告