angular 5, RxJs { map } 导入不起作用或者我遗漏了什么? [英] angular 5, RxJs { map } import doesn't work or i'm missing something?

查看:21
本文介绍了angular 5, RxJs { map } 导入不起作用或者我遗漏了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试映射 httpclient 的结果,我们需要使用 RxJs 的新导入来使 treeshaking 工作.

I'm trying to map the result of my httpclient and we need to use the new import for RxJs to get the treeshaking working.

所以我找到了 2 张地图,但没有用...

so i've found 2 map but none work...

import { map } from 'rxjs/operator/map'; 
import { map } from 'rxjs/operators/map';

我们需要删除的旧时尚方式

the old fashion way that we need to remove

import 'rxjs/add/operator/map';

这是我开始工作所需的代码!

Here is the code i need to get to work!

  getValues(): Observable<Value[]> {   
    return this.http.get<Response<Values>>(this.url).map(reponse => {
      return reponse.data.values;
    });
  }

但是 .map 并不以可观察的方式而闻名,

but the .map is not known for the observable,

推荐答案

导入 RxJS 操作符的正确现代"方式是:

The proper "modern" way to import RxJS operators is:

import { map } from 'rxjs/operators';

连同使用管道操作符.

您的代码变为:

getValues(): Observable<Value[]> {   
  return this.http.get<Response<Values>>(this.url).pipe(
    map(reponse => reponse.data.values)
  );
}

这篇关于angular 5, RxJs { map } 导入不起作用或者我遗漏了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆