RxJS merge

2020-10-13 18:45 更新

创建一个输出 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>

也可以看看

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号