我正在开发一个通过API请求与服务器通信的Angular应用程序.
我有一个组件存储从服务器接收的对象集合.目前,在每个CRUD操作(创建、读取、更新、删除)之后,我再次从服务器获取包括这个修改/添加对象的整个对象集合.
我的问题是
在每次CRUD操作之后,从服务器获取整个elenet集合是否被认为是一种正常的做法,或者最好使用修改/添加的对象本地更新客户端集合.
示例代码如下.
谢谢
class SampleComponent {
collection: any = [];
constructor(private sampleService: SampleService) {
}
// Approach 1
addElement(newElement: any) {
this.sampleService.addElement(newElement).subscribe(e => {
this.sampleService.findAllElements(collection => this.collection = collection);
});
}
// Approach 2
addElement(newElement: any) {
this.sampleService.addElement(newElement).subscribe(e => {
this.collection.push(e);
});
}
}