簡體   English   中英

NgRx 在開始時從效果中調用動作

[英]NgRx calling actions from effects at the beginning

我正在嘗試在效果之前或開始時發送一個動作。

小例子:

saveSomething$ = createEffect(() =>
    this.actions$.pipe(
      ofType(SaveProjectAction),
      tap(() => ImSavingNowAction()), // trigger action here
      withLatestFrom(this.store.select(selectSomething)),
      mergeMap(([action, project]) => {
        let save$ = this.api.call(something); // Do an api call or something that takes long here


        return save$.pipe(
          map(
            (project) => SavedSomethingAction({ something }),// Next Action to be called
            catchError(() => EMPTY)
          )
        );
      })
    )
  );

我認為最好在調度第一個動作后直接在reducer中處理它,而無需從effect調度ImSavingNowAction動作。

一般來說,最佳實踐實際上是處理一個或多個動作,並將它們 map 處理為一個單獨的動作,而不在其中調度任何其他action

您可以查看no-dispatch-in-effectsno-multiple-actions-in-effects規則以獲取更多詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM