如何在整个应用程序中保持数据同步? [英] How to keep your data in sync throughout your application?

查看:218
本文介绍了如何在整个应用程序中保持数据同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多组件可以显示服务提供的单个或多个产品.

I have many components displaying single or multiple products provided by a service.

getProducts() {
    return this.http.get('/api/products');
}

getProduct(id) {
    return this.http.get('/api/product/'+id);
}

在某些组件中,我可以使用此服务添加新产品或对其进行编辑.

In some components, I can add new products or edit them using this service.

saveProduct(product) {
    if(product._id) {
        return this.http.put('/api/product/'+product._id, product);
    }

    return this.http.post('/api/product', product);
}

这些组件同时显示,但不能直接相互连接. 因此,每当发生更改时,我都需要将此更改通知所有其他组件.

These components are displayed simultaneously but not directly connected to each other. So, whenever a change occurs, I need to notify all other components about this change.

我认为可以观察到的方法是可行的,但是我不确定如何开始.

I think observables are the way to go, but I'm not sure how to get started.

在整个应用程序中维护数据的正确方法是什么?

What is the right way to keep data maintained throughout my app?

推荐答案

您正在寻找的是一家商店.最著名的是 Redux ,几乎在每个React应用程序中都使用它. Angular有自己的替代方案,基于Redux,名为 ngrx存储.

What you are looking for is a store. The most famous one is Redux which is used in nearly every React application. Angular has its own alternative, based on Redux and called ngrx store.

这篇关于如何在整个应用程序中保持数据同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆