返回值后多次调用ngrx效果

时间:2019-08-02 06:12:23

标签: javascript angular typescript rxjs ngrx

在返回值后多次调用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();
          }));
      }
    ));
  });

3 个答案:

答案 0 :(得分:2)

您应该添加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 };
  });

来自document

的示例
  logActions$ = createEffect(() =>
    this.actions$.pipe(
      tap(action => console.log(action))
    ), { dispatch: false });

答案 1 :(得分:2)

?这不是我第一次看到此问题,因为这是一个易于制作和忽略的问题。

这就是为什么我在ngrx-tslint-rules中为其添加一条规则以防止将来出现的原因。

答案 2 :(得分:-1)

由于 createEffect 函数。您应该删除包装器createEffect函数,而仅使用this.actions $ .pipe。这样可以解决您的问题。我已经做了,现在可以了。

loadMovies$ = this.actions$.pipe(
    ofType(counterActions.CounterActionTypes.IncrementCounter),
    flatMap(() => {
        return this.userService.counter()
          .pipe(
              map(movies => {
                  return new counterActions.IncrementCounter();
              })
          );
    })
);