亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript中的observables?操作符創(chuàng)建實(shí)例

 更新時(shí)間:2022年03月16日 14:45:34   作者:掘金安東尼  
這篇文章主要介紹了JavaScript中的observables?操作符創(chuàng)建實(shí)例的相關(guān)資料,文章介紹詳細(xì),需要的小伙伴可以參考一下,希望對(duì)你有所幫助

操作符是 observables 背后的馬力,為復(fù)雜的異步任務(wù)提供了一種優(yōu)雅的聲明式解決方案。

本篇就帶領(lǐng)大家 “粗略” 過(guò)一下 observable 創(chuàng)建實(shí)例的重點(diǎn) API 都有哪些?以及用代碼片段展示出用法示意~

一、創(chuàng)建實(shí)例

1.??create??

??create?? 肯定不陌生吧?使用給定的訂閱函數(shù)來(lái)創(chuàng)建 observable ;

// RxJS v6+
import { Observable } from 'rxjs';
/*
? 創(chuàng)建在訂閱函數(shù)中發(fā)出 'Hello' 和 'World' 的 observable 。
*/
const hello = Observable.create(function(observer) {
? observer.next('Hello');
? observer.next('World');
});

// 輸出: 'Hello'...'World'
const subscribe = hello.subscribe(val => console.log(val));

2.??empty??

??empty?? 會(huì)給我們一個(gè)空的 observable,如果我們訂閱這個(gè) observable ,它會(huì)立即發(fā)送 complete 的訊息;

var source = Rx.Observable.empty();

source.subscribe({
? ? next: function(value) {
? ? ? ? console.log(value)
? ? },
? ? complete: function() {
? ? ? ? console.log('complete!');
? ? },
? ? error: function(error) {
? ? ? ? console.log(error)
? ? }
});
// complete!

3.??from?? 

用 ??from?? 來(lái)接收任何可列舉的參數(shù)(JS 數(shù)組);

// RxJS v6+
import { from } from 'rxjs';

// 將數(shù)組作為值的序列發(fā)出
const arraySource = from([1, 2, 3, 4, 5]);
// 輸出: 1,2,3,4,5
const subscribe = arraySource.subscribe(val => console.log(val));

4.??of??

與 ??from?? 相似的 ??of??,也是用于操作一個(gè) list,按順序發(fā)出任意數(shù)量的值;

// RxJS v6+
import { of } from 'rxjs';

// 依次發(fā)出提供的任意數(shù)量的值
const source = of(1, 2, 3, 4, 5);
// 輸出: 1,2,3,4,5
const subscribe = source.subscribe(val => console.log(val));

5.??fromEvent??

??fromEvent?? 將事件轉(zhuǎn)換成 observable 序列;

// RxJS v6+
import { fromEvent } from 'rxjs';
import { map } from 'rxjs/operators';

// 創(chuàng)建發(fā)出點(diǎn)擊事件的 observable
const source = fromEvent(document, 'click');
// 映射成給定的事件時(shí)間戳
const example = source.pipe(map(event => `Event time: ${event.timeStamp}`));
// 輸出 (示例中的數(shù)字以運(yùn)行時(shí)為準(zhǔn)): 'Event time: 7276.390000000001'
const subscribe = example.subscribe(val => console.log(val));

6.??fromPromise??

??fromPromise?? 創(chuàng)建由 promise 轉(zhuǎn)換而來(lái)的 observable,并發(fā)出 promise 的結(jié)果;

var source = Rx.Observable
? .fromPromise((resolve, reject) => {
? ? setTimeout(() => {
? ? ? resolve('Hello RxJS!');
? ? },3000)
? })

// 等同于
var source = Rx.Observable
? .from(new Promise((resolve, reject) => {
? ? setTimeout(() => {
? ? ? resolve('Hello RxJS!');
? ? },3000)
? }))

7.??interval??

顯然,??interval?? 操作和時(shí)間有關(guān),它基于給定時(shí)間間隔發(fā)出數(shù)字序列;

// RxJS v6+
import { interval } from 'rxjs';

// 每1秒發(fā)出數(shù)字序列中的值
const source = interval(1000);
// 數(shù)字: 0,1,2,3,4,5....
const subscribe = source.subscribe(val => console.log(val));

// import { interval } from 'rxjs';
// const source = interval(1000);
// 等同于
// var source = Rx.Observable.interval(1000);

8.??timer??

??timer?? 是 ??interval?? 的升級(jí),用于給定持續(xù)時(shí)間后,再按照指定間隔時(shí)間依次發(fā)出數(shù)字。

// RxJS v6+
import { timer } from 'rxjs';

/*
? timer 接收第二個(gè)參數(shù),它決定了發(fā)出序列值的頻率,在本例中我們?cè)?秒發(fā)出第一個(gè)值,
? 然后每2秒發(fā)出序列值
*/
const source = timer(1000, 2000);
// 輸出: 0,1,2,3,4,5......
const subscribe = source.subscribe(val => console.log(val));

到此這篇關(guān)于JavaScript中的observables 操作符創(chuàng)建實(shí)例的文章就介紹到這了,更多相關(guān)observables 操作符創(chuàng)建實(shí)例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論