Angular 如何将解析程序返回的数据合并到CombineTest调用中?
我在页面上有一个解析器来加载数据。此解析器返回一个可观察的Angular 如何将解析程序返回的数据合并到CombineTest调用中?,angular,rxjs,observable,resolver,Angular,Rxjs,Observable,Resolver,我在页面上有一个解析器来加载数据。此解析器返回一个可观察的 然后,我使用CombineTest将来自解析器的流与另一个流相结合 categorySelectedSubject = new Subject<number>(); categorySelectedAction$ = this.categorySelectedSubject.asObservable(); ngOnInit(): void { this.productsWithCategoriesResolved$ =
然后,我使用CombineTest将来自解析器的流与另一个流相结合
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)
}
但问题是,当我组合流时,我得到一个错误,说我的流在初始化之前就被使用了。我试图将CombineTest放入一个函数中,并在从解析器获得数据后调用它。然后,在CombineTest中的products.filter调用上出现一条错误消息“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)
}
categorySelectedSubject=新主题();
categorySelectedAction$=this.categorySelectedSubject.asObservable();
ngOnInit():void{
this.productsWithCategoriesResolved$=this.route.snapshot.data[“resolvedData”]
}
this.filteredByCategoryProducts$=CombineTest([
此.productsWithCategoriesResolved$,
this.categorySelectedAction$
.烟斗(
startWith(0)
)
])
.烟斗(
地图(([产品,所选类别ID])=>
products.filter(p=>selectedCategoryId?p.categoryId===selectedCategoryId:true)
),
catchError(err=>{
this.errorMessage=err;
返回空;
})
);
onCategorySelected(categoryId:string){
this.categorySelectedSubject.next(+categoryId)
}
非常感谢您的帮助或建议。
CombineTest
运算符需要所有可观察对象,当所有可观察对象发出至少一个值时,它将调用subscribe。这里有this.route.snapshot.data[“resolvedData”]
保存着产品集合(Product[]
)
您必须转换filteredByCategoryProducts$
以保持Product[]
的可观察性,只需添加of
操作符以转换流即可
import { combineLatest, of } from 'rxjs';
this.filteredByCategoryProducts$ = of(this.route.snapshot.data["resolvedData"]);
combineLatest
操作符需要所有可观察对象,当所有可观察对象发出至少一个值时,它调用subscribe。这里有this.route.snapshot.data[“resolvedData”]
保存着产品集合(Product[]
)
您必须转换filteredByCategoryProducts$
以保持Product[]
的可观察性,只需添加of
操作符以转换流即可
import { combineLatest, of } from 'rxjs';
this.filteredByCategoryProducts$ = of(this.route.snapshot.data["resolvedData"]);
在Activatedroute上使用observable,而不是snapshot.data。然后使用rxjs Pull操作符可以提取所需的数据
this.productsWithCategoriesResolved$ = this.route.data.pipe(pluck('resolvedData'));
在Activatedroute上使用observable,而不是snapshot.data。然后使用rxjs Pull操作符可以提取所需的数据
this.productsWithCategoriesResolved$ = this.route.data.pipe(pluck('resolvedData'));
看起来你可以只做
这个.productsWithCategoriesResolved$.pipe(过滤器(布尔))
。看起来你可以只做这个.productsWithCategoriesResolved$.pipe(过滤器(布尔))
。是的!非常感谢Pankaj Parkar!它起作用了!我只是一个初学者与观测,所以,再次感谢!对非常感谢Pankaj Parkar!它起作用了!我只是一个初学者与观测,所以,再次感谢!