[英]How to combine data returned by a resolver into a combineLatest call?
我在页面上有一个解析器来加载数据。 这个解析器返回一个Observable<Product[]>
然后我使用 combineLatest 将来自解析器的流与另一个流合并。
但问题是当我组合流时,我收到一个错误,说我的流在初始化之前就被使用了。 我尝试将 combineLatest 放入一个函数中,并在从解析器获取数据后调用它。 然后我在 combineLatest 中的 products.filter 调用中收到一条错误消息“过滤器不是函数”。
categorySelectedSubject = new Subject<number>();
categorySelectedAction$ = this.categorySelectedSubject.asObservable();
ngOnInit(): void {
this.productsWithCategoriesResolved$ = this.route.snapshot.data["resolvedData"]
}
this.filteredByCategoryProducts$ = combineLatest([
this.productsWithCategoriesResolved$,
this.categorySelectedAction$
.pipe(
startWith(0)
)
])
.pipe(
map(([products, selectedCategoryId]) =>
products.filter(p => selectedCategoryId ? p.categoryId === selectedCategoryId : true)
),
catchError(err => {
this.errorMessage = err;
return EMPTY;
})
);
onCategorySelected(categoryId: string){
this.categorySelectedSubject.next(+categoryId)
}
非常感谢您的任何帮助或建议。
combineLatest
运算符需要所有可观察对象,并且当所有可观察对象发出值至少为 1 时,它就会调用 subscribe。 在这里,您有this.route.snapshot.data["resolvedData"]
持有产品集合( Product[]
)。
你必须转换filteredByCategoryProducts$
持有的可观察的Product[]
刚刚加入of
运营商转换流。
import { combineLatest, of } from 'rxjs';
this.filteredByCategoryProducts$ = of(this.route.snapshot.data["resolvedData"]);
使用 Activatedroute 上可观察到的数据而不是 snapshot.data。 然后使用 rxjs pluck operator 您可以提取所需的数据。
this.productsWithCategoriesResolved$ = this.route.data.pipe(pluck('resolvedData'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.