Rxjs ';属于';vs';从';操作人员
Rxjs ';属于';vs';从';操作人员,rxjs,Rxjs,Observable.of和Observable.from参数格式之间的唯一区别是什么?像函数.prototype.call和函数.prototype.apply Observable.of(1,2,3).subscribe(() => {}) Observable.from([1,2,3]).subscribe(() => {}) 不完全是。当将数组传递给Observable.from时,它与Observable.of的唯一区别是传递参数的方式 但是,Observable.fro
Observable.of
和Observable.from
参数格式之间的唯一区别是什么?像函数.prototype.call
和函数.prototype.apply
Observable.of(1,2,3).subscribe(() => {})
Observable.from([1,2,3]).subscribe(() => {})
不完全是。当将数组传递给
Observable.from
时,它与Observable.of
的唯一区别是传递参数的方式
但是,Observable.from
将接受
要转换的可下标对象、承诺、类可观察对象、数组、可观察对象或类数组对象
对于的可观测性没有类似的行为,它总是只接受值,不执行转换。在传递类似数组的结构(包括字符串)时,必须注意的和的之间的区别: 将立即打印整个阵列 另一方面,
Observable.from([1, 2, 3]).subscribe(x => console.log(x));
逐1打印元素
对于字符串,行为是相同的,但在字符级别。另一个有趣的事实是可以观察到的。([])在订阅时将是一个空数组。 当您订阅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())
....
如果没有可保存的站点,即;data.length=0在addSite节中,上述代码将返回Observable.of([]),然后转到保存注释。但是如果将其替换为Observable.from([]),则后续方法将不会被调用
一行差异:
let fruits = ['orange','apple','banana']
from:从阵列中逐个发射项目。比如说
from(fruits).subscribe(console.log) // 'orange','apple','banana'
of(fruits).subscribe(console.log) // ['orange','apple','banana']
of:立即发射整个阵列。比如说
from(fruits).subscribe(console.log) // 'orange','apple','banana'
of(fruits).subscribe(console.log) // ['orange','apple','banana']
注意:的运算符可以作为的运算符与扩展运算符
of(...fruits).subscribe(console.log) // 'orange','apple','banana'
from
:从数组、promise或iterable创建可观察的。只接受一个值。对于数组、iterables和字符串,所有包含的值都将作为序列发出
const值=[1,2,3];
从(值);//1.2.3.
of
:创建具有可变数量的值的可观察对象,按顺序发出值,但将数组作为单个值
const值=[1,2,3];
(值“hi”,4,5);//[1, 2, 3] ... '嗨。。。4.5.
来自的操作符获取事件源<代码>来自(源)
运算符的获取单个事件<代码>的(事件1、事件2、事件3)
从操作员处,可接受以下各项之一: 许诺 可迭代的 阵列 可观察 从可见光发射每个单独的项目,也可以进行转换 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))
如果可以观察到(1,2,3)的话该怎么办?订阅(x=>console.log(x))@小柯那么肯定是三个独立的排放(1,然后2,然后3)。
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))