[英]How to combine data returned by a resolver into a combineLatest call?
I have a resolver on a page to load the data.我在页面上有一个解析器来加载数据。 This resolver returns an
Observable<Product[]>
这个解析器返回一个
Observable<Product[]>
Then I combine the stream from the resolver with another stream using combineLatest.然后我使用 combineLatest 将来自解析器的流与另一个流合并。
But the problem is when I combine the streams, I get an error that says my streams are used before being initialized.但问题是当我组合流时,我收到一个错误,说我的流在初始化之前就被使用了。 I tried to put the combineLatest into a function and call it after I got the data from the resolver.
我尝试将 combineLatest 放入一个函数中,并在从解析器获取数据后调用它。 I then have an error message "filter is not a function" on the products.filter call in the 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)
}
Thanks a lot for any help or suggestions.非常感谢您的任何帮助或建议。
combineLatest
operator requires all observables, and when all observerables emit value at least one then it calls subscribe. combineLatest
运算符需要所有可观察对象,并且当所有可观察对象发出值至少为 1 时,它就会调用 subscribe。 Over here you have this.route.snapshot.data["resolvedData"]
holding collection of products ( Product[]
).在这里,您有
this.route.snapshot.data["resolvedData"]
持有产品集合( Product[]
)。
You have to convert filteredByCategoryProducts$
to hold an Observable of Product[]
just by adding of
operator to convert stream.你必须转换
filteredByCategoryProducts$
持有的可观察的Product[]
刚刚加入of
运营商转换流。
import { combineLatest, of } from 'rxjs';
this.filteredByCategoryProducts$ = of(this.route.snapshot.data["resolvedData"]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.