[英]error TS2339: Property 'map' does not exist on type 'Observable<Response>'
I am trying to get data from mongodb, for which I have written a service. 我正在尝试从mongodb获取数据,我为此编写了一项服务。 But I am getting an error like
error TS2339: Property 'map' does not exist on type 'Observable<Response>'
但我得到一个错误,如
error TS2339: Property 'map' does not exist on type 'Observable<Response>'
Please help me to resolve this error... 请帮我解决这个错误...
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class DataService {
result:any;
constructor(private _http: Http) { }
getUsers() {
return this._http.get("/api/users")
.map(result => this.result = result.json().data);
}
}
You have to import and use the map
operator differently: 您必须以不同方式导入和使用
map
运算符:
Change 更改
import 'rxjs/add/operator/map';
to 至
import { map } from 'rxjs/operators';
Then, do 然后做
return this._http.get("/api/users")
.pipe(map(result => this.result = result.json().data));
Migrate from the Http service to the HttpClient. 从Http服务迁移到HttpClient。 see migration guide
请参阅迁移指南
To update to
HttpClient
, you'll need to replaceHttpModule
withHttpClientModule
from@angular/common/http
in each of your modules, inject theHttpClient
service, and remove anymap(res => res.json())
calls, which are no longer needed.要更新到
HttpClient
,您需要在每个模块中用@angular/common/http
替换HttpModule
和HttpClientModule
,注入HttpClient
服务,并删除任何map(res => res.json())
调用,这些调用是不再需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.