为什么`combineLatest`返回OperatorFunction< {},number> [英] Why `combineLatest` returns OperatorFunction<{}, number>
问题描述
我正在使用 RxJS 6
,我的代码中有两个可观察对象
I am using RxJS 6
I have two observables in my code
import { combineLatest } from 'rxjs/operators';
loading$: Observable<boolean>;
loaded$: Observable<boolean>;
可观察对象不是来自 http
请求,而是来自 store
The observables are not coming from http
request but from the store
我需要根据以下逻辑将序列组合/转换为一个可观察的值:
如果序列 true
, true
或 false
, false
-需要返回新的可观察到的 true
,否则需要返回 false
I need to combine/convert the sequences into one single observable value based on this logic:
If the sequence true
, true
or false
, false
- need to return new observable true
otherwise need to return false
我试图使用 combineLatest
来实现这一目标:
I tried to use combineLatest
to achieve that:
combineLatest(
this.loaded$, this.loading$,
(val1, val2) => Number(val1) + Number(val2) === 1
)
但是问题是我的 combineLatest
返回'OperatorFunction<>
而不是 Observable<>
,所以我无法订阅.您对如何解决这个问题有任何想法吗?还是可以采用其他方法?
But the problem is that my combineLatest
returns 'OperatorFunction<>
and not Observable<>
so I cannot subscribe.
Do you have any ideas on how to fix that? Or if there is another approach that can be taken?
更新:
似乎是我的问题的答案 import {CombineLatest} from'RxJS';
目前正在测试
UPDATE:
Seems like that is the answer to my question import { combineLatest } from 'RxJS';
currently testing
推荐答案
使用RxJS 5.5的可能性最大,因为您需要使用来自 rxjs/operators
的 combineLatest
将其用作可观察的创建方法",这意味着您需要使用 rxjs/observable/combineLatest
.
With RxJS 5.5 it's most likely because you're using combineLatest
from rxjs/operators
while you want to use it as an Observable "creation method" which means you need to use rxjs/observable/combineLatest
.
我的意思是你需要使用这个:
I mean you need to use this:
import { combineLatest } from 'rxjs/observable/combineLatest';
...代替这个:
import { combineLatest } from 'rxjs/operators';
编辑:由于您提到使用RxJS 6,因此可以从 rxjs
导入静态变体.
Since you mentioned you're using RxJS 6 you can import the static variant from rxjs
.
import { combineLatest } from 'rxjs';
如果您正在寻找 combineLatest
运算符,则必须像这样导入它:
If you're looking for combineLatest
operator you have to import it like this:
import { combineLatest } from 'rxjs/operators';
这篇关于为什么`combineLatest`返回OperatorFunction< {},number>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!