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();
输出
作者:terry,如若转载,请注明出处:https://www.web176.com/rxjs/1935.html