使用 Ngrx Effects Lifecycle hooks OnInitEffects(似乎无限发射)
Using Ngrx Effects Lifecycle hooks OnInitEffects (seems to emit infinitely)
我正在尝试学习一些关于 Ngrx 7 中引入的效果生命周期挂钩的知识,但我并没有真正理解正在发生的事情。我有一个 Angular 应用程序,我在效果 class 中有以下内容,但是,init$ observable 正在无限发射值。我原以为它会触发一次并完成。我对 Observables 也有些陌生。该文档并没有真正帮助我,因为没有太多示例。我可以添加一个 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'};
}
}
这是预期的行为 - 效果的主要用途是作用于某些第三方副作用。然后你设置 ofType<Action>
所以它会在任何 Action 发生时发出,而对于 ex:
@Effect({dispatch: false})
login$ = this.actions$.pipe(
ofType<LoginAction>(AuthActionTypes.LoginAction),
tap(console.log)
);
每当 LoginAction 发生时发出。
我正在尝试学习一些关于 Ngrx 7 中引入的效果生命周期挂钩的知识,但我并没有真正理解正在发生的事情。我有一个 Angular 应用程序,我在效果 class 中有以下内容,但是,init$ observable 正在无限发射值。我原以为它会触发一次并完成。我对 Observables 也有些陌生。该文档并没有真正帮助我,因为没有太多示例。我可以添加一个 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'};
}
}
这是预期的行为 - 效果的主要用途是作用于某些第三方副作用。然后你设置 ofType<Action>
所以它会在任何 Action 发生时发出,而对于 ex:
@Effect({dispatch: false})
login$ = this.actions$.pipe(
ofType<LoginAction>(AuthActionTypes.LoginAction),
tap(console.log)
);
每当 LoginAction 发生时发出。