【问题标题】:Ngrx: How to subscribe to a few actions in component?Ngrx:如何订阅组件中的一些操作?
【发布时间】:2021-03-11 22:48:22
【问题描述】:

我需要订阅一些动作并将它们合二为一,否则会导致重复弹出消息。

  ngOnInit(): void {
    forkJoin([
      this.actions.pipe(ofType(userActions.updateUserFail)),
      this.actions.pipe(ofType(brandActions.updateBrandFail)),
    ]).subscribe(() => {
      this.toastr.error(userInfoFailMsg);
    });
  }

但这不起作用。

【问题讨论】:

    标签: observable action ngrx ngrx-store


    【解决方案1】:

    ForkJoin 仅在所有源 observables 完成时才发出值,请参阅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);
        });
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 2018-11-03
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 2022-11-10
      • 2020-04-27
      相关资源
      最近更新 更多