我如何返回RXJ中Allproducts $的IEMPHOSERES的IEMPHOSERES?
我想从具有Allproducts $的匹配productName的GetEmployeeById函数中返回可观察的可观察。
allProducts$ = this.state$.pipe(
map(x => x.allProducts as IProducts[] || [] as IProducts[]),
distinctUntilChanged());
GetEmployeeByID(Id: number): Observable<IEmployees> {
return this.allEmployees$.pipe(
map(res => <IEmployees>res.find(data => data.id === Id) ?? { id: -1, name: "N/A", email: "N/A", gender: "N/A", productid: -1, productName: "N/A" } as IEmployees),
switchMap((Employee: IEmployees) => {
return this.allProducts$.subscribe(allProducts => {
return of({
...Employee,
productName: allProducts.find(Product => Product.productid == Employee.productid)?.name
}) as IEmployees
})
})
)
}
I want to return Observable from GetEmployeeByID function that has a matching productName from allProducts$:
allProducts$ = this.state$.pipe(
map(x => x.allProducts as IProducts[] || [] as IProducts[]),
distinctUntilChanged());
GetEmployeeByID(Id: number): Observable<IEmployees> {
return this.allEmployees$.pipe(
map(res => <IEmployees>res.find(data => data.id === Id) ?? { id: -1, name: "N/A", email: "N/A", gender: "N/A", productid: -1, productName: "N/A" } as IEmployees),
switchMap((Employee: IEmployees) => {
return this.allProducts$.subscribe(allProducts => {
return of({
...Employee,
productName: allProducts.find(Product => Product.productid == Employee.productid)?.name
}) as IEmployees
})
})
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与其订阅
allproducts $
switchmap 内部的,通过它在管道内部执行所需的操作,然后返回其结果可观察到的。有关上下文,请参见下面的代码。Instead of subscribing
allProducts$
insideswitchMap
, pipe through it, do what you need inside the pipe, and return its resulting observable. See the code below for context.