当我尝试合并observbles时,我收到错误You provided 'undefined' where a stream was expected.
。
我在这里创建我的刷新observable:
refresh$: Observable<Funnel[]>;
然后我有一个计时器......
TimerObservable.create(0, (this.storageService.refRate * 1000))
.takeWhile(() => this.interval)
.subscribe(() => {
this.refresh$ = this.funnelService.getStoryFunnels({}).map(response => [...response]);
});
然后当我尝试合并observable时,会产生错误:
this.funnels$ = this.funnelService.getStoryFunnels({}).pipe(
merge(this.refresh$, funnelCreation$.pipe(
filter(funnel => !!funnel),
map(funnel => [funnel])
)
)
);
基于舒适的回答进行更新:
this.refresh$ = TimerObservable.create(0, (this.storageService.refRate * 500))
.takeWhile(() => this.interval)
.switchMap(() => this.funnelService.getStoryFunnels({}).map(response => [...response]));
然后:
this.funnels$ = this.funnelService.getStoryFunnels({}).pipe(
merge(this.refresh$, funnelCreation$.pipe(
filter(funnel => !!funnel),
map(funnel => [funnel])
)
)
);
答案 0 :(得分:2)
这是因为this.funnels$
在声明时不是可观察的(不是用任何值启动),因为它只会在定时器之后才被赋值被执行。然而,声明代码几乎立即执行(同步)。这就是为什么你得到undefined
。
您应该使用switchMap
并将this.refresh$
分配给它:
this.refresh$ = TimerObservable.create(0, (this.storageService.refRate * 1000))
.takeWhile(() => this.interval)
.switchMap(() => this.funnelService.getStoryFunnels({}).map(response => [...response]));