ngrx 效果在返回值后被多次调用

Vij*_*esh 4 javascript rxjs typescript ngrx angular

ngrx 效果在返回值后被多次调用。

loadMovies$: Observable<Action> = createEffect(() => {
    return this.actions$.pipe(
    ofType(counterActions.CounterActionTypes.IncrementCounter),
    flatMap(() => {
        return this.userService.counter()
        .pipe(
          map(movies => {
             return new counterActions.IncrementCounter();
          }));
      }
    ));
  });
Run Code Online (Sandbox Code Playgroud)

Ton*_*Ngo 5

你应该添加dispatch: false你的效果

  loadMovies$ = createEffect(() => {
    this.actions$.pipe(
    ofType(counterActions.CounterActionTypes.IncrementCounter),
    flatMap(() => {
        return this.userService.counter()
        .pipe(
          map(movies => {
             return new counterActions.IncrementCounter();
          }));
      }
    )),
    { dispatch: false };
  });
Run Code Online (Sandbox Code Playgroud)

文档中的示例

  logActions$ = createEffect(() =>
    this.actions$.pipe(
      tap(action => console.log(action))
    ), { dispatch: false });
Run Code Online (Sandbox Code Playgroud)