链接两个服务器调用一个NgEffects

时间:2017-06-21 14:15:14

标签: angular ngrx ngrx-effects ngrx-store

我正在使用ngrx,我不知道如何在一个ngEffect中链接两个服务器调用。如果你有任何想法,我需要你的帮助。

      @Effect() patternsSources$ :Observable<Action> = this.actions$
    .ofType(GET_PATTERN_SOURCE_ACTION)
    .switchMap(action => this.service.getPatternSource(action.payload))
    .map((patternsData:Sources) =>  new GetPatternsSourcesResponse(patternsData))


  @Effect() patternsList$ :Observable<Action> = this.actions$
    .ofType(GET_PATTERN_LIST_ACTION)
    .switchMap(action => this.service.getPatternList(action.payload))
    .map((data) => new GetPatternsListResponse(data));

在此行为中,第一个呼叫是interrupeted,只处理第二个呼叫

修改

减速器功能

export function storeData(state:UiStoreData=INITIAL_STORE_DATA, action:Action) : UiStoreData {
  switch(action.type) {
    case GET_PATTERN_SOURCE_RESPONSE_ACTION :
      return handleGetPatternSourcesResponse(state,action as GetPatternsSourcesResponse);
    case GET_PATTERN_LIST_RESPONSE_ACTION:
      return handleGetPatternListResponse(state,action as GetPatternsListResponse);
    default:
      return state;
  }
}

和派遣行动

export class AppComponent {
  constructor(public store:Store<ApplicationState>) {
    this.store.dispatch(new GetPatternsSourcesAction('/sources'));
    this.store.dispatch(new GetPatternsListAction('/rules'));

  }
}

编辑

export class GetPatternsSourcesAction implements Action {
  type = GET_PATTERN_SOURCE_ACTION
  constructor(public payload?:string) {}
}

export class GetPatternsSourcesResponse implements Action {
  type = GET_PATTERN_SOURCE_RESPONSE_ACTION;
  constructor(public payload?:Sources) {}
}

export class GetPatternsListAction implements Action {
  type: string = GET_PATTERN_LIST_ACTION;
  constructor(public payload?:string) {}
}

export class GetPatternsListResponse implements Action {
  type: string = GET_PATTERN_LIST_RESPONSE_ACTION;
  constructor(public payload?:PatternList) {}
}

1 个答案:

答案 0 :(得分:0)

这听起来不像是一个调度问题。

看起来像你的方法:

handleGetPatternSourcesResponse(state,action as GetPatternsSourcesResponse);

handleGetPatternListResponse(state,action as GetPatternsListResponse);

在州的同一部分开展业务。我认为没有别的方法来判断'我在reducer中得到相同的数据'是有道理的。 所以,你应该在状态中有两个不同的位置:patternslist和patternSources,每个完整的动作应该相应地在reducer中处理。 这样你以后可以做store.select(partYouNeed)。