Angular 2 ngrx:具有一个http请求的多个减速器?
在Angular 2应用程序中,我正在尝试实现ngrx,因此我的一个组件中包含以下代码:Angular 2 ngrx:具有一个http请求的多个减速器?,angular,redux,rxjs,ngrx,Angular,Redux,Rxjs,Ngrx,在Angular 2应用程序中,我正在尝试实现ngrx,因此我的一个组件中包含以下代码: OnInit() { this.myService.getApplicationList(); } 该服务返回一个json: { Count : 25, // count of all applications Applications[Array] // array with applications objects in it Categories[Array] // array of cat
OnInit() {
this.myService.getApplicationList();
}
该服务返回一个json:
{
Count : 25, // count of all applications
Applications[Array] // array with applications objects in it
Categories[Array] // array of categories
}
如何在仅提出一个请求的情况下将计数、应用程序和类别分配给各自的减速机
我现在要做的是将响应对象映射到response.json()应用程序
Myservice.ts
getApplicationList(limit: number) {
return this.http.get(this.baseUrl)
.map(res => res.json().Applications )
.map(payload => ({ type: 'GET_APP', payload : payload }))
.subscribe(action => {
this.store.dispatch(action)});
}
对于获取类别,我正在执行与前面相同的请求:
getApplicationList(limit: number) {
return this.http.get(this.baseUrl)
.map(res => res.json().Categories)
.map(payload => ({ type: 'GET_CATEGORIES', payload : payload }))
.subscribe(action => {
this.store.dispatch(action)});
}
我如何简化这种重复,并将我的应用程序和类别数组映射到只有一个http请求?您可以在订阅功能中处理这两个问题
getApplicationList(limit: number) {
return this.http.get(this.baseUrl)
.map(res => res.json())
.subscribe(res => {
this.store.dispatch({ type: 'GET_CATEGORIES', payload : res.Categories });
this.store.dispatch({ type: 'GET_APP', payload : res.Applications });
});
}
您可以在subscription函数中处理这两个问题
getApplicationList(limit: number) {
return this.http.get(this.baseUrl)
.map(res => res.json())
.subscribe(res => {
this.store.dispatch({ type: 'GET_CATEGORIES', payload : res.Categories });
this.store.dispatch({ type: 'GET_APP', payload : res.Applications });
});
}
我认为更正确的方法是利用ngrx@Effect()
- 使用两个属性设置一个名为AppState的状态;类别和应用李>
- 创建一个类似“LOAD_APP”的操作,效果应该侦听该操作,然后一旦触发,将触发http请求,然后发送一个名为LOAD_APP_SUCCESS的操作,并将负载作为响应,然后将负载分别存储在store AppState道具中
- 使用两个属性设置一个名为AppState的状态;类别和应用李>
- 创建一个类似“LOAD_APP”的操作,效果应该侦听该操作,然后一旦触发,将触发http请求,然后发送一个名为LOAD_APP_SUCCESS的操作,并将负载作为响应,然后将负载分别存储在store AppState道具中
- 我认为更正确的方法是利用ngrx@Effect()