【问题标题】:Error: property 'subscribe' does not exist on type 'OperatorFunction<{}, {} | Observable<any>>错误:类型 'OperatorFunction<{}、{} 上不存在属性 'subscribe' |可观察的<任何>>
【发布时间】:2019-06-06 20:41:39
【问题描述】:

为什么我不能订阅 concat() 运算符的结果,即使它看起来与文档和示例中的相同 https://rxjs-dev.firebaseapp.com/api/index/function/concat

我已经尝试更改为 concatMap 或 concatAll 但我有更多问题,此解决方案是迄今为止我发现的最优雅的解决方案。

ngOnInit() {
    const trendingRentals = this.rentalService.getRentals().pipe(delay(500));
    const commonRentals = this.rentalService.getCommonRentalsTest().pipe(delay(1000));
    const luxuryRentals = this.rentalService.getLuxuryRentalsTest().pipe(delay(1500));

    const concatenation =  concat( [trendingRentals, commonRentals, luxuryRentals]);
    concatenation.subscribe(
      //Print result
    )
  }

    //All get request are equal but with a different endpoint
    public getCommonRentalsTest(): Observable<any> {
      return this.http.get('/api/v1/rentals/common');
    }

加快的结果是按顺序获取所有租金,然后以相同的顺序将它们推送到结果数组。

【问题讨论】:

标签: javascript angular typescript asynchronous rxjs


【解决方案1】:

看起来您需要'static' factory function 而不是concat operator

// Find:
import { concat } from 'rxjs/operators';

// Replace:
import { concat } from 'rxjs';

该运算符与另一个 observable 的 pipe 方法一起使用。例如:

const stream1$: Observable<any> = ...;
const stream2$: Observable<any> = ...;

const concatenated = stream1$.pipe(
    concat(stream2$)
);

【讨论】:

  • 你先生是神 :') 非常感谢你!!!!所以基本上我必须写类似 const stream1$: Observable = //My http request function const concatenated = stream1$:pipe( concat(stream2$, stream3$,..., streamX$) ) 或 const concatated = stream1$:pipe( concat(stream2$), //..... concat(streamX$), ) 谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-09
  • 2018-11-03
  • 2018-01-29
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
相关资源
最近更新 更多