我正在使用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));
在此行为中,第一个呼叫被中断,仅处理第二个呼叫
编辑
减速机功能
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) {}
}