【发布时间】:2020-03-25 17:38:40
【问题描述】:
我使用 typicode/json-server 创建了自己的 db.json 服务器,看起来像这样 -
{
"cart": [
{
"cartId": 1,
"products": [
{ "pId": 1, "name": "aaa", "quantity":.. },
{ "pId": 2, "name": "bbb", "quantity":.. }
]
},
{
"cartId": 2,
"products": [{ "pId": 2, "name": "bbb", "quantity":.. }]
}
]
}
这是将产品添加到购物车的方法:
addProductToCart((productObj:any)) {
this.getCart() // uses POST, reference '/cart', returns observable
.subscribe(cartId => {
this.getProducts(cartId, productObj.pId) // uses GET, reference '/cart/cartId/pId', returns observable
.subscribe(product => {
// update properties of product here..
});
});
}
我是 Angular 的新手,我知道嵌套的 observables 不是最佳实践。
如何避免使用嵌套的 observable?
有没有办法用不同的方法编写相同的方法?
【问题讨论】:
-
以下是关于可观察对象链接的答案:stackoverflow.com/questions/49596641/… 这可能会有所帮助。
标签: angular observable angular8