码:
const cold = new Observable((observer: Observer<any>) => {
observer.next(Math.random());
});
const hot = cold.pipe(share());
hot.subscribe(a => console.log('a: ' + a));
hot.subscribe(b => console.log('b: ' + b));
}
预期结果-a和b具有相同的值:
// a:0.17919353301075858
// b:0.17919353301075858
实际结果-仅在浏览器控制台中获得a的值:
// a:0.07958207844185083
任何想法?
你可以尝试shareReplay(1)
以重播最后一个值
https://www.learnrxjs.io/learn-rxjs/operators/multicasting/sharereplay
另外,一篇好文章:https : //itnext.io/the-magic-of-rxjs-sharing-operators-and-their-differences-3a03d699d255
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句