RxJS - 多播运算符 share



它是一个 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);

输出

share Operator
广告