Ngrx:如何订阅组件中的一些动作?
Ngrx: How to subscribe to a few actions in component?
我需要订阅几个动作并将它们作为一个动作,否则会导致重复弹出消息。
ngOnInit(): void {
forkJoin([
this.actions.pipe(ofType(userActions.updateUserFail)),
this.actions.pipe(ofType(brandActions.updateBrandFail)),
]).subscribe(() => {
this.toastr.error(userInfoFailMsg);
});
}
但这行不通。
ForkJoin 仅在所有源可观察对象完成时才发出值,请参见https://www.learnrxjs.io/learn-rxjs/operators/combination/forkjoin。
根据定义,操作是永无止境的流。我想你想要的是:
https://www.learnrxjs.io/learn-rxjs/operators/combination/merge
像这样:
ngOnInit(): void {
merge([
this.actions.pipe(ofType(userActions.updateUserFail)),
this.actions.pipe(ofType(brandActions.updateBrandFail)),
]).subscribe(userInfoFailMsg => {
this.toastr.error(userInfoFailMsg);
});
}
对于动作,这实际上也应该有效:
ngOnInit(): void {
this.actions.pipe(ofType(
userActions.updateUserFail,
brandActions.updateBrandFail)
).subscribe(userInfoFailMsg => {
this.toastr.error(userInfoFailMsg);
});
}
我需要订阅几个动作并将它们作为一个动作,否则会导致重复弹出消息。
ngOnInit(): void {
forkJoin([
this.actions.pipe(ofType(userActions.updateUserFail)),
this.actions.pipe(ofType(brandActions.updateBrandFail)),
]).subscribe(() => {
this.toastr.error(userInfoFailMsg);
});
}
但这行不通。
ForkJoin 仅在所有源可观察对象完成时才发出值,请参见https://www.learnrxjs.io/learn-rxjs/operators/combination/forkjoin。
根据定义,操作是永无止境的流。我想你想要的是: https://www.learnrxjs.io/learn-rxjs/operators/combination/merge 像这样:
ngOnInit(): void {
merge([
this.actions.pipe(ofType(userActions.updateUserFail)),
this.actions.pipe(ofType(brandActions.updateBrandFail)),
]).subscribe(userInfoFailMsg => {
this.toastr.error(userInfoFailMsg);
});
}
对于动作,这实际上也应该有效:
ngOnInit(): void {
this.actions.pipe(ofType(
userActions.updateUserFail,
brandActions.updateBrandFail)
).subscribe(userInfoFailMsg => {
this.toastr.error(userInfoFailMsg);
});
}