Angular 2 ngrx/store根据另一个可观测服务调用的结果调用可观测服务调用

Angular 2 ngrx/store根据另一个可观测服务调用的结果调用可观测服务调用,angular,rxjs,observable,ngrx,ngrx-effects,Angular,Rxjs,Observable,Ngrx,Ngrx Effects,我正在努力学习和理解Rxjs和ngrx/store 我有两个api调用,第一个获取字符名数组,第二个获取字符清单 如何使用ngrx/store action/reducers/effects通过第一个函数返回的数组运行第二个函数 以下是服务功能: public getCharacterNames(): Observable<string[]> { return this._http.get(this._url + '/characters?access_token=' + thi

我正在努力学习和理解Rxjs和ngrx/store

我有两个api调用,第一个获取字符名数组,第二个获取字符清单

如何使用ngrx/store action/reducers/effects通过第一个函数返回的数组运行第二个函数

以下是服务功能:

public getCharacterNames(): Observable<string[]> {
  return this._http.get(this._url + '/characters?access_token=' + this._key)
    .map((res: Response) => res.json())
    .catch((error: any) => Observable.throw(error.json().error || 'server error'));
}

public getCharactersInventory(characterName: string): Observable<Bag[]> {
  return this._http.get(this._url + '/characters/' + encodeURI(characterName) + '/inventory?access_token=' + this._key)
    .map((res: Response) => res.json())
    .catch((error: any) => {return Observable.throw(error.json().error || 'server error'); });
}
这是减速器:

  case Gw2Actions.LOAD_CHARACTERS_SUCCESS: {
    return action.payload;
  }
这就是我尝试使用的效果:

@Effect() private loadCharacters$: Observable<Action> = this._actions$
  .ofType(Gw2Actions.LOAD_CHARACTERS)
  .map((action) => action.payload)
  .switchMap(() => this._gw2Service.getCharacterNames())
  .map((characterNames) => {
      let characters = [];
      characterNames.forEach((characterName) => {
        let characterBags = this._gw2Service.getCharactersInventory(characterName)
          .subscribe((res) => res);
        characters.push({
          name: characterName,
          bags: characterBags
        });
      });
      return characters;
    }
  )
  .map((characters: Character[]) => this._gw2Actions.loadCharactersSuccess(characters));
@Effect()private loadCharacters$:Observable=this.\u actions$
.of类型(Gw2Actions.LOAD_字符)
.map((操作)=>action.payload)
.switchMap(()=>this.\u gw2Service.getCharacterNames())
.map((字符名称)=>{
让字符=[];
characterNames.forEach((characterName)=>{
让characterBags=this.\u gw2Service.getCharactersInventory(characterName)
.认购((res)=>res);
角色推送({
名称:characterName,
书包:人物书包
});
});
返回字符;
}
)
.map((字符:Character[])=>this.\u gw2Actions.loadCharactersAccess(字符));

我建议将Rxjs和ngstore这两个挑战分开。下面是一种从字符名解析清单的方法。希望您能够将其映射到您的ngstore效果中

let charNames$=Rx.Observable.of([{name:'a'},{name:'b'},{name:'c'}])//假名字
让getInventoryFromName=name=>Rx.Observable.of({owner:name}).delay(1000)//模拟库存
让结果$=charNames$.switchMap(names=>Rx.Observable.forkJoin(names.map(n=>getInventoryFromName(n.name)));
结果$.subscribe(res=>console.log('got some inventory/user pairs:',res))

我建议将Rxjs和ngstore这两个挑战分开。下面是一种从字符名解析清单的方法。希望您能够将其映射到您的ngstore效果中

let charNames$=Rx.Observable.of([{name:'a'},{name:'b'},{name:'c'}])//假名字
让getInventoryFromName=name=>Rx.Observable.of({owner:name}).delay(1000)//模拟库存
让结果$=charNames$.switchMap(names=>Rx.Observable.forkJoin(names.map(n=>getInventoryFromName(n.name)));
结果$.subscribe(res=>console.log('got some inventory/user pairs:',res))

.map((characterNames)
部分对我来说似乎是错误的,但无法确定需要在那里做什么:|
.map((characterNames)
part对我来说似乎是错误的,但无法确定需要在那里做什么:|我尝试实现类似的东西,但无法使其工作..我想我遗漏了或误解了一些东西我尝试实现类似的东西,但无法使其工作..我想我遗漏了或误解了一些东西
@Effect() private loadCharacters$: Observable<Action> = this._actions$
  .ofType(Gw2Actions.LOAD_CHARACTERS)
  .map((action) => action.payload)
  .switchMap(() => this._gw2Service.getCharacterNames())
  .map((characterNames) => {
      let characters = [];
      characterNames.forEach((characterName) => {
        let characterBags = this._gw2Service.getCharactersInventory(characterName)
          .subscribe((res) => res);
        characters.push({
          name: characterName,
          bags: characterBags
        });
      });
      return characters;
    }
  )
  .map((characters: Character[]) => this._gw2Actions.loadCharactersSuccess(characters));