Angular .map无法从HttpClient访问

Angular .map无法从HttpClient访问,angular,http,rxjs,httpclient,angular-services,Angular,Http,Rxjs,Httpclient,Angular Services,我正在学习一个教程,但它有点过时,所以我试着补充一下。目前,这些服务看起来像 import { Http } from '@angular/http'; export class TodoService { constructor(private http: Http) { } add(todo) { return this.http.post('...', todo).map(r => r.json()); } getTodos() { re

我正在学习一个教程,但它有点过时,所以我试着补充一下。目前,这些服务看起来像


import { Http } from '@angular/http';


export class TodoService {
  constructor(private http: Http) {
  }

  add(todo) {
    return this.http.post('...', todo).map(r => r.json());
  }

  getTodos() {
    return this.http.get('...').map(r => r.json());
  }

  delete(id) {
    return this.http.delete('...').map(r => r.json());
  }
}
但是它说
找不到模块'@angular/http'或其相应的类型声明。ts(2307)

所以我把它改成了

  constructor(private http: HttpClient) {
但是现在我得到了.map,带有
属性“map”的红色下划线在类型“Observable”上不存在。ts(2339)

我试着从“rxjs/operators”导入{map}
导入'rxjs/add/operator/map'但我仍然得到错误

我如何配置它在不重写this.http.post/get/delete行的情况下工作


谢谢你的帮助

map是一个rxjs操作符,您可以这样使用它:

import { map } from "rxjs/operators";
return this.http.post('...', todo).pipe(
 map(r => r.json())
);
当您使用不推荐的angular/http时,您可以使用HttpClient

import { HttpClient } from '@angular/common/http';

谢谢你的澄清!但它还没有完全发挥作用。通过您的更改,我现在在
.json
上遇到了一个错误。有什么想法吗?我认为不需要将其解析为json,如果您使用的是httpClient,那么delete.json,只需返回响应即可。json是.map的一部分,因此如果我删除它,它不会违背拥有.map的目的吗?这不是只剩下
返回this.http.post(“…”,todo)
了吗?唯一的问题是组件正在映射响应,所以如果我删除.map,那么我会得到一个错误,如果gettoos像这样
gettoos(){返回this.http.get(“…”);}
然后订阅它的组件尝试定义返回的数据类型gettoos():Observable{return this.http.get(“…”);}