defaultIfEmpty

函数签名: defaultIfEmpty(defaultValue: any): Observable

如果在完成前没有发出任何通知,那么发出给定的值

示例

示例 1: 没有值的 Observable.of 的默认值

( Stackblitz | jsBin | jsFiddle )

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

// 当源 observable 为空时,发出 'Observable.of() Empty!',否则发出源的任意值
const exampleOne = of().pipe(defaultIfEmpty('Observable.of() Empty!'));
// 输出: 'Observable.of() Empty!'
const subscribe = exampleOne.subscribe(val => console.log(val));
示例 2: Observable.empty 的默认值

( Stackblitz | jsBin | jsFiddle )

// RxJS v6+
import { defaultIfEmpty } from 'rxjs/operators';
import { empty } from 'rxjs';

// 当源 observable 为空时,发出 'Observable.empty()!',否则发出源的任意值
const example = empty().pipe(defaultIfEmpty('Observable.empty()!'));
// 输出: 'Observable.empty()!'
const subscribe = example.subscribe(val => console.log(val));

其他资源


:file_folder: 源码: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/defaultIfEmpty.ts

results matching ""

    No results matching ""