【问题标题】:Conditionally trigger only one stream RXJS有条件地只触发一个流 RXJS
【发布时间】:2019-12-26 15:00:03
【问题描述】:

我有一个BehaviorSubjectaccountClicked$

每当点击帐户时都会调用它。现在,每当任何帐户更改时,都会有另外两个流依赖于它。他们被称为

用户信息$ & linkingDetailsByAccount$

它工作正常,直到这里。用户单击该帐户,然后依次触发这两个流,这两个流按预期工作。现在,在一个新的用例中,我只想要

用户信息$

linkingDetailsByAccount$ 不在该特定场景中运行。我可以复制整个流并这样做,但这并不好,并且需要我不想要的复制。

非常感谢任何建议。

谢谢, 瓦特萨尔

【问题讨论】:

  • 1. “叫”是什么意思? 2.它们是如何“联系起来”的?您可以使用 filter() 运算符过滤掉事件,但我需要知道您是如何链接您的 observables 以确保
  • 我能够在过滤器操作员的帮助下解决这个问题。但是,我不得不介绍像“JustUserInfo”这样的事件类型。因此每次都会填充用户信息,但其他流将按此类型过滤。因此,如果类型是“JustUserInfo”,他们不会做任何事情。如果还有其他更好的方法,我会很高兴知道的
  • 如果可以的话,我会很感激一些代码。
  • @Stavm - 我刚刚发布了解决方案,代码为 sn-p。请看一下

标签: angular rxjs observable


【解决方案1】:

在过滤器操作员的帮助下,我能够弄清楚。但是,我不得不介绍像“JustUserInfo”这样的事件类型。 所以基本上每次都会填充用户信息,但其他流将按此类型过滤。因此,如果类型是“JustUserInfo”,他们不会做任何事情。

private getAccountDetailsClickedSubject = new BehaviorSubject<any>({
  type: 'Init',
  account: ''
});

private getAccountDetailsClicked$ = 
this.getAccountDetailsClickedSubject.asObservable();

private getTrimmedAccountDetailsClicked$ = this.getAccountDetailsClicked$.pipe(
  filter(({ account }) => account.trim() !== '')
);

private getFilteredJustUserTypeAccount$ = 
this.getTrimmedAccountDetailsClicked$.pipe(
  filter(({ type }) => type !== 'JustUserInfo')
);

userInfo$ = this.getTrimmedAccountDetailsClicked$.pipe(tap(console.log))

linkingDetailsByAccount$: Observable<any> = 
 this.getFilteredJustUserTypeAccount$.pipe(tap(console.log))
);

//To just get user info
this.getAccountDetailsClickedSubject.next({type: 'JustUserInfo',account});

//To get everything

this.getAccountDetailsClickedSubject.next({type: 'Init',account});

如果有其他更好的方法,我会很高兴知道的

谢谢,

瓦特萨尔

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    相关资源
    最近更新 更多