欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

map Property 'map' does not exist on type 'Observable<any>' 有大用 有大大用

const birthday$ = birthday.valueChanges.map(d=>{
      return {date:d,from:'birthday'};
    });

1)  import { map } from 'rxjs/operators';

2)  把 map() 改成 pipe(map())



map

函数签名: map(project: Function, thisArg: any): Observable

对源 observable 的每个值应用投射函数。

示例

示例 1: 每个数字加10

StackBlitz | jsBin | jsFiddle )

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

// 发出 (1,2,3,4,5)
const source = from([1, 2, 3, 4, 5]);
// 每个数字加10
const example = source.pipe(map(val => val + 10));
// 输出: 11,12,13,14,15
const subscribe = example.subscribe(val => console.log(val));
示例 2: 映射成单一属性

StackBlitz | jsBin | jsFiddle )

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

// 发出 ({name: 'Joe', age: 30}, {name: 'Frank', age: 20},{name: 'Ryan', age: 50})
const source = from([
  { name: 'Joe', age: 30 },
  { name: 'Frank', age: 20 },
  { name: 'Ryan', age: 50 }
]);
// 提取每个 person 的 name 属性
const example = source.pipe(map(({ name }) => name));
// 输出: "Joe","Frank","Ryan"
const subscribe = example.subscribe(val => console.log(val));

相关食谱

其他资源


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


来自  https://rxjs-cn.github.io/learn-rxjs-operators/operators/transformation/map.html

普通分类: