该combineLatest功能可以从进口rxjs和从rxjs /运营商。
当我从rxjs / operators导入它时(就像我导入CombineAll一样,我得到以下错误:
TS2339: Property 'subscribe' does not exist on type 'OperatorFunction<{}, [{}, number, number, number]>'
Run Code Online (Sandbox Code Playgroud)
我使用了以下代码片段:
import { timer } from "rxjs";
import { combineLatest } from "rxjs/operators";
const timerOne = timer(1000, 2500);
const timerTwo = timer(1500, 2500);
const timerThree = timer(2000, 2500);
//when one timer emits, emit the latest values from each timer as an array
const combined$ = combineLatest(timerOne, timerTwo, timerThree);
combined$.subscribe(
([timerValOne, timerValTwo, timerValThree]) => console.log(`Timer One Latest: ${timerValOne}, Two Latest: ${timerValTwo}, Three Latest: ${timerValThree}`)
);
Run Code Online (Sandbox Code Playgroud)
因此,我尝试从rxjs而不是rxjs / operators导入它:
import { combineLatest } from "rxjs";
Run Code Online (Sandbox Code Playgroud)
突然间它起作用了。很好,但是谁能解释两者之间的区别是什么?
import { combineLatest } from "rxjs";
这就是所谓的“可观察的创建方法”。基本上是一种基于您传递的参数返回Observable的方法(就像from()或of())。由于它返回一个实例,Observable因此具有subscribe()方法。
import { combineLatest } from "rxjs/operators";
这是应该在运算符链中使用的运算符。这是一个方法,该方法返回预订前一个Observable的另一个方法,并返回另一个处理在其输出中经历的每个值的Observable。
这就是为什么它会给您错误Property 'subscribe' does not exist on type....。subscribe()从combineLatest()运算符返回的方法上不存在该方法(它返回另一个函数)。
| 归档时间: |
|
| 查看次数: |
571 次 |
| 最近记录: |