【问题标题】:rxjs call combineLatest in combineLatestrxjs 在 combineLatest 中调用 combineLatest
【发布时间】:2017-06-25 23:46:19
【问题描述】:

rxjscombineLatest 方法有问题。 我试图在combineLatest 中调用combineLatest,但它不起作用,尽管它返回 Observable 对象。请帮忙解决问题。 console.log 永远不会被调用

实际上,不同文件中的所有观察者,所以我无法将this.search$移动到this.services$

this.search$ = store.select('search');

this.services$ = Observable.combineLatest(
    store.select('currentRegions'),
    store.select('services'),
    (regions, services) => {
        // some filter here
        return services;
    }
);

this.autocomplete$ = Observable.combineLatest(
    this.search$,
    this.services$,
    (search, services) => {
        console.log('show me, please');
        return '';
    }
);

已解决:没有订阅者无法使用,所以我必须订阅它

【问题讨论】:

  • 大部分observables 都被称为“冷”,这意味着当您创建它们时,它们在您订阅之前不会运行。

标签: angular rxjs rxjs5 ngrx


【解决方案1】:

combineLatest() 运算符在其所有源 Observable 发出至少一个值时发出一个值。

因此,如果 console.log(...) 从不打印,则意味着 this.search$this.services$ 从不发出任何内容。换句话说,这意味着store.select('currentRegions')store.select('services')store.select('search') 之一永远不会发出任何值。

请注意,您可以使用startWith()(即使使用startWith(null))。

这可行:https://jsbin.com/gecede/2/edit?js,console

这不起作用:https://jsbin.com/livafar/2/edit?js,console

【讨论】:

  • 但如果我将 console.log 写入this.services$ 回调,它将适用于任何编辑
  • @Hyper 查看我的更新,其中包含两个应该模拟您的情况的示例。
【解决方案2】:

RxJS CombineLatest 是一种 AngularJS $q.all

import 'rxjs/add/observable/combineLatest';


Observable
      .combineLatest(this.store.select('currentRegions'), this.store.select('services')
      .do(console.log)
      .subscribe();

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 2019-01-08
    • 2016-06-29
    • 2019-03-03
    • 1970-01-01
    • 2021-05-09
    • 2017-03-27
    • 2018-03-29
    • 2018-04-18
    相关资源
    最近更新 更多