2017-11-29 14 views
0

こんにちは私は "loadFullService $"の新しいアクション "LoadConfig"をディスパッチしたいと思います。1つのエフェクトから複数のアクションをディスパッチする

どのようにすればいいですか?コンストラクタで

@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))); 
    }); 

Thxをuが

答えて

0

インポートストアサービス。

どこでも ofType()後のアクション呼び出し this.store.dispatch(newAction)doオペレータ(通常)と、内部次に
constructor(
    private store: Store<StoreType>, 
) 

@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))); 
 
    });

+0

私が試したが、私は実際に何をすべきでしょうか? .do(this.store.dispatch(新しいservicesActions.LoadConfig(action.id、action.name))))を実行しますか? –

+0

はい、回答を更新しました –

+0

Thx u!最後の質問私は "LoadFullService"アクションの戻り値である "name"が必要ですが、戻すことはできますか? –

関連する問題