Rxjs 为什么共享的可观察对象在第一个完成时重新订阅
当我运行下面的代码时,我得到了期望的结果,然后在另一个可观察对象完成时,重新订阅延迟的共享可观察对象Rxjs 为什么共享的可观察对象在第一个完成时重新订阅,rxjs,rxjs-observables,Rxjs,Rxjs Observables,当我运行下面的代码时,我得到了期望的结果,然后在另一个可观察对象完成时,重新订阅延迟的共享可观察对象 const source = interval(1000).pipe( take(5), share()); source.subscribe(x => console.log('c1', x)); source.pipe( delay(2000), // doesn't delay subscription buffers the output from int
const source = interval(1000).pipe(
take(5),
share());
source.subscribe(x => console.log('c1', x));
source.pipe(
delay(2000), // doesn't delay subscription buffers the output from interval observable
switchMapTo(source)
).subscribe(x=>console.log('c2',x))
输出:
C10
c1 1
c1 2
c1 3
c2 3
c1 4
c2 4
c2 0c2 1 c2 2 c2 3
c2 4这是递归的<代码>源排放导致源订阅
source.pipe(//最终“订阅”到源
delay(2000),//不延迟订阅缓冲来自interval observable的输出
switchMapTo(源)//每次发射后再次订阅源
也许你想要的是:
timer(2000).pipe(//最终订阅一个计时器
switchMapTo(源)//在2s后订阅源(计时器发出)
);
看起来您已经修改了答案中的代码。source.pipe(delay(2000),switchMapTo(source))
的行为将与of(null.pipe(delay(2000),switchMapTo(source))非常不同
。因为delay
对其源的订阅在源完成时结束:switchMapTo
在此之后发生。