codecamp

RxJS merge

创建一个输出 Observable,该输出同时从每个给定的输入 Observable 发出所有值。

merge<T, R>(...observables: any[]): Observable<R>

参量

可观察的 输入可观察对象以合并在一起。

returns

Observable<R>:一个 Observable,它发出作为每个输入 Observable 的结果的项目。

描述

通过将多个 Observable 的值混合到一个 Observable中,将其平展在一起。

合并大理石图

merge订阅每个给定的输入 Observable(作为参数),并简单地将所有值从所有输入 Observable 转发(不进行任何转换)到输出 Observable。仅当所有输入 Observable 完成后,输出 Observable 才完成。输入 Observable 传递的任何错误将立即在输出 Observable 上发出。

例子

合并两个 Observable:1s 间隔和点击

import { merge, fromEvent, interval } from 'rxjs';


const clicks = fromEvent(document, 'click');
const timer = interval(1000);
const clicksOrTimer = merge(clicks, timer);
clicksOrTimer.subscribe(x => console.log(x));


// Results in the following:
// timer will emit ascending values, one every second(1000ms) to console
// clicks logs MouseEvents to console everytime the "document" is clicked
// Since the two streams are merged you see these happening
// as they occur.

合并3个 Observable,但只有 2 个同时运行

import { merge, interval } from 'rxjs';
import { take } from 'rxjs/operators';


const timer1 = interval(1000).pipe(take(10));
const timer2 = interval(2000).pipe(take(6));
const timer3 = interval(500).pipe(take(10));
const concurrent = 2; // the argument
const merged = merge(timer1, timer2, timer3, concurrent);
merged.subscribe(x => console.log(x));


// Results in the following:
// - First timer1 and timer2 will run concurrently
// - timer1 will emit a value every 1000ms for 10 iterations
// - timer2 will emit a value every 2000ms for 6 iterations
// - after timer1 hits its max iteration, timer2 will
//   continue, and timer3 will start to run concurrently with timer2
// - when timer2 hits its max iteration it terminates, and
//   timer3 will continue to emit a value every 500ms until it is complete

超载

merge(v1: any, scheduler: SchedulerLike): Observable<T>

参量: 类型:
v1 类型:any
调度器 类型:SchedulerLike

returnsObservable<T>

merge(v1: any, concurrent: number, scheduler: SchedulerLike): Observable<T>

参量: 类型:
v1 类型:any
同时 类型:number
调度器 类型:SchedulerLike

returnsObservable<T>

merge(v1: any, v2: any, scheduler: SchedulerLike): Observable<T | T2>

参量: 类型:
v1 类型:any
v2 类型:any
调度器 类型:SchedulerLike
returnsObservable<T | T2>

merge(v1: any, v2: any, concurrent: number, scheduler: SchedulerLike): Observable<T | T2>

参量: 类型:
v1 类型:any
v2 类型:any
同时 类型:number
调度器 类型:SchedulerLike
returnsObservable<T | T2>

merge(v1: any, v2: any, v3: any, scheduler: SchedulerLike): Observable<T | T2 | T3>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
调度器 类型:SchedulerLike
returnsObservable<T | T2 | T3>

merge(v1: any, v2: any, v3: any, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
同时 类型:number
调度器 类型:SchedulerLike
returnsObservable<T | T2 | T3>

merge(v1: any, v2: any, v3: any, v4: any, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
v4 类型:any
调度器 类型:SchedulerLike
returnsObservable<T | T2 | T3 | T4>

merge(v1: any, v2: any, v3: any, v4: any, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
v4 类型:any
同时 类型:number
调度器 类型:SchedulerLike
returnsObservable<T | T2 | T3 | T4>

merge(v1: any, v2: any, v3: any, v4: any, v5: any, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
v4 类型:any
v5 类型:any
调度器 类型:SchedulerLike
returnsObservable<T | T2 | T3 | T4 | T5>

merge(v1: any, v2: any, v3: any, v4: any, v5: any, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
v4 类型:any
v5 类型:any
同时 类型:number
调度器 类型:SchedulerLike
returnsObservable<T | T2 | T3 | T4 | T5>
merge(v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5 | T6>
参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
v4 类型:any
v5 类型:any
v6 类型:any
调度器 类型:SchedulerLike
returnsObservable<T | T2 | T3 | T4 | T5 | T6>

merge(v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, concurrent: number, scheduler: SchedulerLike): Observable<T | T2 | T3 | T4 | T5 | T6>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
v4 类型:any
v5 类型:any
v6 类型:any
同时 类型:number
调度器 类型:SchedulerLike
returnsObservable<T | T2 | T3 | T4 | T5 | T6>

merge(v1: any): Observable<T>

参量: 类型:
v1 类型:any

returnsObservable<T>

merge(v1: any, concurrent?: number): Observable<T>

参量: 类型:
v1 类型:any
同时 可选的默认值为undefined。  类型:number

returnsObservable<T>

merge(v1: any, v2: any): Observable<T | T2>

参量: 类型:
v1 类型:any
v2 类型:any
returnsObservable<T | T2>

merge(v1: any, v2: any, concurrent?: number): Observable<T | T2>

参量: 类型:
v1 类型:any
v2 类型:any
同时 可选的。默认值为undefined。类型:number
returnsObservable<T | T2>

merge(v1: any, v2: any, v3: any): Observable<T | T2 | T3>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
returnsObservable<T | T2 | T3>

merge(v1: any, v2: any, v3: any, concurrent?: number): Observable<T | T2 | T3>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
同时 可选的。默认值为undefined。类型:number
returnsObservable<T | T2 | T3>

merge(v1: any, v2: any, v3: any, v4: any): Observable<T | T2 | T3 | T4>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
v4 类型:any
returnsObservable<T | T2 | T3 | T4>

merge(v1: any, v2: any, v3: any, v4: any, concurrent?: number): Observable<T | T2 | T3 | T4>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
v4 类型:any
同时 可选的。默认值为undefined。类型:number
returnsObservable<T | T2 | T3 | T4>

merge(v1: any, v2: any, v3: any, v4: any, v5: any): Observable<T | T2 | T3 | T4 | T5>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
v4类型:any。v5类型:any
returnsObservable<T | T2 | T3 | T4 | T5>

merge(v1: any, v2: any, v3: any, v4: any, v5: any, concurrent?: number): Observable<T | T2 | T3 | T4 | T5>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
v4 类型:any
v5 类型:any
同时 可选的。默认值为undefined。类型:number
returnsObservable<T | T2 | T3 | T4 | T5>

merge(v1: any, v2: any, v3: any, v4: any, v5: any, v6: any): Observable<T | T2 | T3 | T4 | T5 | T6>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
v4 类型:any
v5 类型:any
v6 类型:any
returnsObservable<T | T2 | T3 | T4 | T5 | T6>

merge(v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, concurrent?: number): Observable<T | T2 | T3 | T4 | T5 | T6>

参量: 类型:
v1 类型:any
v2 类型:any
v3 类型:any
v4 类型:any
v5 类型:any
v6 类型:any
同时 可选的。默认值为undefined。类型:number
returnsObservable<T | T2 | T3 | T4 | T5 | T6>

merge(...observables: any[]): Observable<T>

参量: 类型:
可观察的 类型:any[]

returnsObservable<T>

merge(...observables: any[]): Observable<T>

参量: 类型:
可观察的 类型:any[]

returnsObservable<T>

merge(...observables: any[]): Observable<R>

参量: 类型:
可观察的 类型:any[]

returnsObservable<R>

merge(...observables: any[]): Observable<R>

参量: 类型:
可观察的 类型:any[]

returnsObservable<R>

也可以看看

RxJS isObservable
RxJS MonoTypeOperatorFunction
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

RxJS operators

RxJS fetch

RxJS testing

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }