mapTo

签名: mapTo(value: any): Observable

将每个发出值映射成常量。

mapTo - 图1

示例

示例 1: 将每个发出值映射成字符串

( StackBlitz |
jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { mapTo } from 'rxjs/operators';
  3. // 每2秒发出值
  4. const source = interval(2000);
  5. // 将所有发出值映射成同一个值
  6. const example = source.pipe(mapTo('HELLO WORLD!'));
  7. // 输出: 'HELLO WORLD!'...'HELLO WORLD!'...'HELLO WORLD!'...
  8. const subscribe = example.subscribe(val => console.log(val));
示例 2: 将点击映射成字符串

( StackBlitz |
jsBin |
jsFiddle )

  1. import { fromEvent } from 'rxjs/observable/fromEvent';
  2. import { mapTo } from 'rxjs/operators';
  3. // 发出每个页面点击
  4. const source = fromEvent(document, 'click');
  5. // 将所有发出值映射成同一个值
  6. const example = source.pipe(mapTo('GOODBYE WORLD!'));
  7. // 输出: (click)'GOODBYE WORLD!'...
  8. const subscribe = example.subscribe(val => console.log(val));

相关食谱

其他资源


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