Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.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
Javascript 角度2+;ngrx/存储:如何更改循环内的状态?_Javascript_Angular_Redux_Ngrx - Fatal编程技术网

Javascript 角度2+;ngrx/存储:如何更改循环内的状态?

Javascript 角度2+;ngrx/存储:如何更改循环内的状态?,javascript,angular,redux,ngrx,Javascript,Angular,Redux,Ngrx,在使用ngrx/store的Angular2应用程序中,我有一个方法可以从API查询一些数据并将其发送到应用程序商店。工作正常: get(id: number) { return this.http.get(`http://localhost:3000/contents/${id}.json`).map( response => { return response.json(); }) .map( (content: Content) => { this.

在使用ngrx/store的Angular2应用程序中,我有一个方法可以从API查询一些数据并将其发送到应用程序商店。工作正常:

get(id: number) {
  return this.http.get(`http://localhost:3000/contents/${id}.json`).map( response => {
    return response.json();
  })
  .map( (content: Content) => {
    this.store.dispatch({ type: 'GET_CONTENT', payload: content });
    return content;
  });
}
我还有另一个服务,查询API并返回一个数组os对象。在这种情况下,我正在这样做:

return this.http.get(`http://localhost:3000/contents/${id}/contents.json`).map( response => {
  return response.json();
})
.map( ( contents: Array<Content>) => {
  contents.map( payload => {
    this.store.dispatch({ type: 'GET_CONTENT', payload });
  });
  return contents;
})
返回this.http.get(`http://localhost:3000/contents/${id}/contents.json`).map(响应=>{
返回response.json();
})
.map((内容:数组)=>{
contents.map(有效负载=>{
dispatch({type:'GET_CONTENT',payload});
});
返回内容;
})

它可以工作,但存储的状态会多次更改。有更好的方法吗?可能会创建另一个动作(“获取内容”),同时保存商店中的所有内容?或者我可以说,只有在“contents.map”结束后才应该更改状态吗?

一个简单的解决方案是编辑您的减速机,以期望使用
数组
负载。然后,在单个
内容的情况下
只需在数组中分派该值:

this.store.dispatch({ type: 'GET_CONTENT', [content] });

一个简单的解决方案是编辑您的减速机以期望
阵列
有效负载。然后,在单个
内容的情况下
只需在数组中分派该值:

this.store.dispatch({ type: 'GET_CONTENT', [content] });

状态应该是一个纯函数才能正常工作,你的函数是纯函数吗?状态应该是一个纯函数才能正常工作,你的函数是纯函数吗?