【问题标题】:Can I create a brand new stream from the existing stream in rxjs我可以从 rxjs 中的现有流创建一个全新的流吗
【发布时间】:2020-07-23 10:04:40
【问题描述】:

例如,我有一个数字流,例如 1、2、3、4 等等。我想感知这些数据中的每一个,并且每当我想在另一个数据流中发出真值时。保持源数据 stram[1,2,3,4] 不变。

【问题讨论】:

  • 你的问题缺乏信息,一点也不清楚。
  • 当然可以
  • @noririco,例如,我有一个流,有许多数字,比如 1,2,3,4 等等,我想在它甚至保留源数据时发出新数据 stram[1 ,2,3,4] 原样。
  • 我知道我可以在另一个 SubjectObservable 的帮助下做到这一点。点击源流中每个发出的数据并检查条件,然后根据该主题发出新数据。
  • 你的问题有点不清楚,但我认为你应该阅读冷和热 Observables 之间的区别,你读过它here

标签: rxjs rxjs-observables


【解决方案1】:

我建议您分享您的来源并订阅两次。


  ...

  private source$ = of(1,2,3,4,5,6).pipe(share());
  
  private evenNumberObservable$ = this.source$.pipe(
    map(x => x % 2 === 0),
    filter(x => !!x)
  );

  //or
  //private evenNumberObservable$ = this.source$.pipe(
  //  filter(x => x % 2 === 0),
  //  map(x => true)
  //);

  public ngOnInit() {
    this.evenNumberObservable$.subscribe(x => console.log(x));
    this.source$.subscribe(x => console.log(x))
  }

  ...

整个code

【讨论】:

    猜你喜欢
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2019-10-05
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    相关资源
    最近更新 更多