在组件安装中,我使用rxfire来组合来自火药库的数据:
store$.pipe(
withLatestFrom(items$),
map(([store, items]) => ({ items, ...store })),
).pipe(
withLatestFrom(categories$),
map(([store, categories]) => ({ categories, ...store })),
).subscribe(store => {
this.setState({ store: store, isLoading: false });
}) 这段代码的问题不是触发,而是屏幕被加载组件卡住了。如果我第二次加载屏幕,它就能工作了。我做错了什么吗?我怎样才能修好它?
发布于 2021-07-24 06:48:59
如果它在重新加载之前无法工作,则可能会根据items$和categories$何时发出它们自己的值而出现计时问题。
将这些可观测数据组合在一起的一种更安全的方法是使用来自combineLatest()的RxJS特性。它接受一个可观测的数组,直到从接收到每个可观察到的值后才会发出。
combineLatest([store$, items$, categories$]).pipe(
map(([store, items, categories])=>({...store, ...items, ...categories})),
tap(store=>this.setState({ store, isLoading: false })
).subscribe();请记住combineLatest(),如果数组中任何可观察到的值都不发出值,那么combineLatest()将永远不会发出值。
https://stackoverflow.com/questions/68506314
复制相似问题