RxJS - 多播运算符 publishReplay
publishReplay 使用行为主题,其中它可以缓冲值并将其重播给新订阅者,并返回 ConnectableObservable。必须使用 connect() 方法来订阅所创建的 observable。
语法
publishReplay(value); // here value is the number of times it has to replay.
示例
import { interval} from 'rxjs'; import { take, publishReplay} from 'rxjs/operators'; let observer = interval(1000).pipe( take(3), publishReplay(2) ); 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(); setTimeout(() => { const subscribe_three = observer.subscribe( x => console.log("Value from Sub3 = "+x) ); }, 2000);
输出
广告