angular 2 个问题“地图在可观察类型上不存在”

angular 2 question 'map does not exist on type observable"

我正在浏览 YouTube 上的 angular 2 教程,但出于某种原因我收到此错误 "map does not exist on type observable"。我做了一些阅读,看到我应该添加最后一个导入行,但没有完全解决它。我看到了一些关于管道的东西,但不确定如何相应地修改代码。谢谢。

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class GithubgithubService {

  constructor(private http: Http) {}

  getUser(searchText): Observable < any > {
    const url = 'http://api.github.com/search/users?q=' + searchText;
    return this.http.get(url).map(
      res => {
        const data = res.json();
        console.log(data);
        return data;
      }
    )
  }

}

改用HttpClient。您一开始就不需要 map 回复。

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class GithubgithubService {

  constructor(private http: HttpClient) {}

  getUser(searchText): Observable<any> {
    const url = 'http://api.github.com/search/users?q=' + searchText;
    return this.http.get(url);
  }

}

确保先将 HttpClientModule 添加到 AppModule 的 imports 数组。