【发布时间】:2018-06-23 21:44:03
【问题描述】:
我有许多组件显示服务提供的单个或多个产品。
getProducts() {
return this.http.get('/api/products');
}
getProduct(id) {
return this.http.get('/api/product/'+id);
}
在某些组件中,我可以使用此服务添加新产品或对其进行编辑。
saveProduct(product) {
if(product._id) {
return this.http.put('/api/product/'+product._id, product);
}
return this.http.post('/api/product', product);
}
这些组件同时显示,但不直接相互连接。 因此,每当发生更改时,我都需要通知所有其他组件有关此更改。
我认为 observables 是要走的路,但我不知道如何开始。
在整个应用程序中维护数据的正确方法是什么?
【问题讨论】:
-
我的应用程序也是一个购物车。在我的应用程序中,所有 CRUD 操作都在 main.js 中完成。我使用 Even Emitter 在购物车中添加和删除产品。这会在不重新加载应用程序的情况下更新总项目、总价格。如果你使用了很多可重用的组件,我建议你使用 resolve。我希望这会有所帮助。
标签: angular typescript observable