我试图将一个 observable 的值分配给另一个 observable 的字段。这是我的代码:
products$.pipe(tap( products => {
this.data$ = this.data$.pipe(map(data => {
data.products = products;
console.dir(data.products);
return data;
}));
}));
您可以使用switchMap
&map
运算符来实现这一点,以使用所需的数据和产品创建新的 observable,如下所示:
const result$ = this.data$.pipe(
switchMap((data) =>
products$.pipe(
map((products) => {
data.products = products;
console.log(data.products);
return data;
})
)
)
);
// Now you can subscribe to the new `result$` observable, and get the data with products:
result$.subscribe((data) => console.log(data.products));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句