Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 将每个请求以角度存储在对象中是正常做法吗?_Angular_Caching - Fatal编程技术网

Angular 将每个请求以角度存储在对象中是正常做法吗?

Angular 将每个请求以角度存储在对象中是正常做法吗?,angular,caching,Angular,Caching,我有一个在对象中存储/缓存每个(或几乎每个)请求的代码,但不确定这是否是一个好的实践 store = []; fetchAll(params: any, cache = true): Observable<PostsInt[]> { let posts = this.http.get<PostsInt[]>(this.getApiUrl('/posts'), { params }); if (cache) { const key = Obje

我有一个在对象中存储/缓存每个(或几乎每个)请求的代码,但不确定这是否是一个好的实践

store = [];
fetchAll(params: any, cache = true): Observable<PostsInt[]> {
 let posts = this.http.get<PostsInt[]>(this.getApiUrl('/posts'), {
   params
 });

 if (cache) {
   const key =
     Object.values(params).join('') + APP_CONFIG.data.defaultLocale;
   if (!this.store[key]) {
     posts = posts.pipe(
       publishReplay(1),
       refCount()
     );
     this.store[key] = posts;
   }
   return this.store[key];
 }
 return posts;
}
store=[];
fetchAll(params:any,cache=true):可观察{
让posts=this.http.get(this.getapirl('/posts'){
params
});
如果(缓存){
常数键=
Object.values(params.join(“”)+APP_CONFIG.data.defaultLocale;
如果(!this.store[key]){
posts=posts.pipe(
出版重播(1),
refCount()
);
this.store[key]=posts;
}
返回此。存储[密钥];
}
返回岗位;
}

您不需要这样做。这取决于负载平衡以及您希望在以后使用这些信息进行的操作。已经有一个类似的答案了。例如:简单的CRUD应用程序不需要存储任何信息,这就是为什么它们是CRUD应用程序。2019年4月发布的另一篇新帖子取决于您使用的框架