使用Ngrx Effects生命周期钩子OnInitEffects(似乎无限发射)

问题描述 投票:0回答:1

我正在尝试学习Ngrx 7中引入的有效生命周期钩子,我并不是真正了解正在发生的事情。我有一个Angular应用程序,我在效果类中有以下内容,但是,init $ observable无限地发出值。我原以为它会发射一次并完成。我对可观察者也有些新意。文档并没有真正帮助我,因为没有太多的例子。我可以添加一个take(1),但我想了解为什么它会继续永远发射。

@Injectable()
export class AuthEffects implements OnInitEffects{

  constructor(private actions$: Actions) {}

  @Effect({dispatch: false})
  login$ = this.actions$.pipe(
    ofType<LoginAction>(AuthActionTypes.LoginAction),
    tap(console.log)
  );

  @Effect({dispatch: false})
  logout$ = this.actions$.pipe(
    ofType<LogoutAction>(AuthActionTypes.LogoutAction),
    tap(console.log)
  );

  @Effect()
  init$ = this.actions$.pipe(
    ofType<Action>('[Auth] Effects Init'),
    tap(console.log)
  );

  ngrxOnInitEffects(): Action {
    console.log('AuthEffects init\'d');
    return { type: '[Auth] Effects Init'};
  }
}
ngrx effects
1个回答
0
投票

这是预期的行为 - 效果的主要用途是对某些第三方副作用起作用。并设置ofType<Action>,以便在任何Action发生时发出,而对于ex:

  @Effect({dispatch: false})
  login$ = this.actions$.pipe(
    ofType<LoginAction>(AuthActionTypes.LoginAction),
    tap(console.log)
  );

只要LoginAction发生,就会发出。

© www.soinside.com 2019 - 2024. All rights reserved.