是可观察对象之间唯一的区别。和Observable.from arguments format?比如Function.prototype.call和Function.prototype.apply?
Observable.of(1,2,3).subscribe(() => {})
Observable.from([1,2,3]).subscribe(() => {})
是可观察对象之间唯一的区别。和Observable.from arguments format?比如Function.prototype.call和Function.prototype.apply?
Observable.of(1,2,3).subscribe(() => {})
Observable.from([1,2,3]).subscribe(() => {})
当前回答
从操作员可以接受其中之一
承诺 可迭代的 数组 可观测的
From从可观察对象中发出每个单独的项,也可以进行转换。
Of操作符接收原始值并从可观察对象中发出该值。
import {from, Observable, of} from 'rxjs';
const ofObs = of([1,2,3]);
const fromObs = from([2,3,4]);
const basicObs = Observable.create(observer=>{
observer.next(100);
observer.next(200);
observer.next(300);
})
const promise = new Promise((resolve,reject)=>{
resolve(100);
})
const array = [1,2,3];
const iterbale = "Dhana";
// const myObs = from(ofObs);//possible and can emit individual item value everytime 1, then ,2 , then 3
// const myObs = from(fromObs);//possbile and can emit individual item value everytime 1, then ,2 , then 3
// const myObs = from(basicObs);//possbile and can emit individual item value everytime 100, then ,200 , then 300
const myObs = from(promise);//possible can emit value 100
// const myObs = array(promise);//possible and can emit individual item value everytime 1, then ,2 , then 3
// const myObs = iterable(promise);//possible and can emit individual item value everytime D then h then a then n then a
myObs.subscribe(d=>console.log(d))
import {from, of} from 'rxjs';
const basicOf1 = of([1,2,3,4,5,6]) // emits entire array of events
const basicfrom1 = from([1,2,3,4,5,6]) //emits each event at a time
const basicOf2 = of(1,2,3,4,5,6) // emits each event at a time
// const basicfrom2 = from(1,2,3,4,5,6) //throws error
//Uncaught TypeError: number is not observable
const basicOf3 = of(...[1,2,3,4,5,6]) // emits each event at a time
const basicfrom3 = from(...[1,2,3,4,5,6]) //throws error
//Uncaught TypeError: number is not observable
basicOf3.subscribe(d=>console.log(d))
这里是codeen的链接
其他回答
在传递一个类数组结构(包括字符串)时,注意of和from之间的区别是很重要的:
Observable.of([1, 2, 3]).subscribe(x => console.log(x));
会立刻打印出整个数组。
另一方面,
Observable.from([1, 2, 3]).subscribe(x => console.log(x));
逐个打印元素。
对于字符串,行为是相同的,但在字符级别。
不完全是。当将数组传递给Observable.from时,它与Observable.from之间唯一的区别是。是参数传递的方式。
然而,Observable.from将接受一个参数
可订阅对象、Promise对象、类observable对象、Array对象、可迭代对象或要转换的类数组对象
Observable没有类似的行为。的,它总是只接受值并且不执行转换。
从操作员可以接受其中之一
承诺 可迭代的 数组 可观测的
From从可观察对象中发出每个单独的项,也可以进行转换。
Of操作符接收原始值并从可观察对象中发出该值。
import {from, Observable, of} from 'rxjs';
const ofObs = of([1,2,3]);
const fromObs = from([2,3,4]);
const basicObs = Observable.create(observer=>{
observer.next(100);
observer.next(200);
observer.next(300);
})
const promise = new Promise((resolve,reject)=>{
resolve(100);
})
const array = [1,2,3];
const iterbale = "Dhana";
// const myObs = from(ofObs);//possible and can emit individual item value everytime 1, then ,2 , then 3
// const myObs = from(fromObs);//possbile and can emit individual item value everytime 1, then ,2 , then 3
// const myObs = from(basicObs);//possbile and can emit individual item value everytime 100, then ,200 , then 300
const myObs = from(promise);//possible can emit value 100
// const myObs = array(promise);//possible and can emit individual item value everytime 1, then ,2 , then 3
// const myObs = iterable(promise);//possible and can emit individual item value everytime D then h then a then n then a
myObs.subscribe(d=>console.log(d))
import {from, of} from 'rxjs';
const basicOf1 = of([1,2,3,4,5,6]) // emits entire array of events
const basicfrom1 = from([1,2,3,4,5,6]) //emits each event at a time
const basicOf2 = of(1,2,3,4,5,6) // emits each event at a time
// const basicfrom2 = from(1,2,3,4,5,6) //throws error
//Uncaught TypeError: number is not observable
const basicOf3 = of(...[1,2,3,4,5,6]) // emits each event at a time
const basicfrom3 = from(...[1,2,3,4,5,6]) //throws error
//Uncaught TypeError: number is not observable
basicOf3.subscribe(d=>console.log(d))
这里是codeen的链接
的值会同时发出所有的值
From将逐一发出所有值
with展开运算符= from运算符
另一个有趣的事实是Observable.of([])在订阅时将是一个空数组。 当你订阅Observable.from([])时,你不会得到任何值。
当您使用switchmap执行连续操作时,这很重要。
例: 在下面的例子中,我将保存一个作业,然后保存站点,然后将注释保存为流。
.do((data) => {
this.jobService.save$.next(this.job.id);
})
.switchMap(() => this.jobService.addSites(this.job.id, this.sites)
.flatMap((data) => {
if (data.length > 0) {
// get observables for saving
return Observable.forkJoin(jobSiteObservables);
} else {
**return Observable.of([]);**
}
})).do((result) => {
// ..
})
.switchMap(() => this.saveComments())
....
如果没有网站可以保存,即;数据。在addSite部分,length = 0,上面的代码返回Observable.of([]),然后保存注释。但是如果你用Observable.from([])替换它,后续的方法将不会被调用。
rxfiddle