我正在youtube上进行角度2教程,由于某种原因,我收到此错误“地图在类型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
数组中。