new Observable 和 of 或 from 有什么区别? [英] What is the Difference between new Observable and of or from?
问题描述
新的 Observable 和从 of 或 from 创建的 observable 有什么区别?
What is the Difference between new Observable and observable created from of or from?
of([1, 2, 3]).subscribe(x => console.log(x));
from([1, 2, 3]).subscribe(x => console.log(x));
vs new Observable()
以上两种创建 observables 的主要区别是什么?
What is the main difference between the above two way of creating observables?
我已经阅读了这篇文章,但还不够令人信服!new Observable()"和of()"在 RxJ 中
I have read this, but it's not yet convincing! what is the difference between "new Observable()" and "of()" in RxJs
我不是在问 of 和 from 之间的区别.我在问(of 或 from)与 new Observable() 的区别
推荐答案
本质上,可观察的创建函数 of
、from
和其他只是创建一个 newObservable()
具有特定行为.
Essentially, the observable creation functions of
, from
, and others simply create a new Observable()
with specific behavior.
因此,new Observable()
和创建者函数之间的唯一区别是实际行为.
So, the only difference between new Observable()
and the creator fuctions is the actual behavior.
例如,of()
函数如下所示(简化):
For example, here is what the of()
function looks like (simplified):
export function of<T>(...array: Array<T>): Observable<T> {
return new Observable<T>(subscriber => {
for (let i = 0; i < array.length && !subscriber.closed; i++) {
subscriber.next(array[i]);
}
subscriber.complete();
});
}
您可以看到 new Observable()
在 of()
函数中被调用.
You can see that new Observable()
is called within the of()
function.
这篇关于new Observable 和 of 或 from 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!