它是multicast()运算符的别名,唯一的区别是您不必手动调用connect()方法即可启动订阅。
语法
share()
例
import { interval} from 'rxjs'; import { take, share} from 'rxjs/operators'; let observer = interval(1000).pipe(take(3), share()); const subscribe_one = observer.subscribe( x => console.log("Value from Sub1 = "+x) ); const subscribe_two = observer.subscribe( x => console.log("Value from Sub2 = "+x) ); setTimeout(() => { const subscribe_three = observer.subscribe( x => console.log("Value from Sub3 = "+x) ); }, 2000);
输出
作者:terry,如若转载,请注明出处:https://www.web176.com/rxjs/1947.html