从一个效果中调度多个动作

时间:2017-11-29 13:43:01

标签: angular ngrx ngrx-store ngrx-effects

您好我想发送我的新动作" LoadConfig" in" loadFullService $"效果。

怎么办?

    @Effect()
loadFullService$: Observable<Action> = this.actions$
    .ofType<servicesActions.LoadFullService>(servicesActions.LOAD_FULL_SERVICE)
    .switchMap(action => {
        return this.apiService
            .loadFullService(action.payload)
            .map((service: Service) => new servicesActions.LoadFullServiceSuccess(service))
            .catch(error => of(new servicesActions.LoadFailureAction(error)));
    })
    ;

@Effect()
loadConfig$: Observable<Action> = this.actions$
    .ofType<servicesActions.LoadConfig>(servicesActions.LOAD_CONFIG)
    .switchMap(action => {
        console.log("action config", action);
        return this.apiService
            .loadConfig(action.id, action.name)
            .map((config: Configuration) => new servicesActions.LoadConfigSuccess(config))
            .catch(error => of(new servicesActions.LoadConfigFailure(error)));
    });

你好

2 个答案:

答案 0 :(得分:1)

在构造函数中导入Store服务。

constructor(
  private store: Store<StoreType>,
)

然后在动作调用this.store.dispatch(newAction)内,使用do运算符(通常),在ofType()之后的任何位置。

@Effect()
loadFullService$: Observable<Action> = this.actions$
  .ofType<servicesActions.LoadFullService>(servicesActions.LOAD_FULL_SERVICE)
  .do(action => {
    this.store.dispatch(new servicesActions.LoadConfig(action.payload.id, action.payload.name))
  })
  .switchMap(action => {
    return this.apiService
      .loadFullService(action.payload)
      .map((service: Service) => new servicesActions.LoadFullServiceSuccess(service))
      .catch(error => of(new servicesActions.LoadFailureAction(error)));
  });

我曾经喜欢的另一种通用方法是创建一个新的可观察对象:

@Effect()
loadFullService$: Observable<Action> = this.actions$
  .ofType<servicesActions.LoadFullService>(servicesActions.LOAD_FULL_SERVICE)
  .switchMap(action => {
    return this.apiService
      .loadFullService(action.payload)
      .mergeMap((service: Service) => {
        return new Observable(observer => {
          const successAction = new servicesActions.LoadFullServiceSuccess(service));
          const newAction = new servicesActions.LoadConfig(action.id, successAction.name));
          observer.next(successAction);
          observer.next(newAction);
          observer.complete();
      });
    })
    .catch(error => of(new servicesActions.LoadFailureAction(error)));
});

缺点是它增加了更多的流失并且随后代码变得有点困难。

最后,第三种方法:

@Effect()
loadFullService$: Observable<Action> = this.actions$
  .ofType<servicesActions.LoadFullService>(servicesActions.LOAD_FULL_SERVICE)
  .switchMap(action => {
    return this.apiService
      .loadFullService(action.payload)
      .mergeMap((service: Service) => {
        const successAction = new servicesActions.LoadFullServiceSuccess(service));
        const newAction = new servicesActions.LoadConfig(action.id, successAction.name));
        return Observable.from([successAction, newAction]);
      })
      .catch(error => of(new servicesActions.LoadFailureAction(error)));
  });

答案 1 :(得分:0)

效果不是只包含一个元素的函数。它们是流转换,您可以在此处使用RxJS的所有功能。例如,要获得标题中要求的内容:

join

注意:这与André的答案中的第三版非常相似。