在 angular2 的 RxJS 中找不到地图

Moh*_*hid 1 rxjs angular

错误:“Observable”类型上不存在属性“map”。

import { Component } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
declare var $: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  testDetails:any;
  constructor(private http: Http) { }

  ngOnInit() {
    this.display();
}
display() {
  this.getTestDetails().subscribe(data => {
      this.testDetails = data;
      console.log((this.testDetails));
  });

}
getTestDetails() {
  return this.http.get('https://randomuser.me/api/')
      .map(res => res.json());
}
  title = 'ecom-project';

}
Run Code Online (Sandbox Code Playgroud)

相同的代码正在其他电脑上运行,但在我的电脑上我遇到错误。我的代码设置缺少什么。

Dav*_*sta 5

您使用什么版本的 RXJS?如果是最新的,那么应该是:

import { map } from "rxjs/operators";
Run Code Online (Sandbox Code Playgroud)

那么你需要做

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

constructor(private http: HttpClient) {}

....this.http.get().pipe(map())
Run Code Online (Sandbox Code Playgroud)

另外,如果您使用的是最新版本的 HttpClient,那么您甚至不需要映射到 JSON,它会自动完成