[英]Angular 4 this.http.get(…).map is not a function
Hi i have problem with my code. 嗨,我的代码有问题。
import { Injectable } from '@angular/core';
import { Car } from "./models/car";
import { Observable } from "rxjs/Observable";
import { Http } from "@angular/http";
import 'rxjs'
@Injectable()
export class CarsService {
private apiUrl = "http://localhost:3000/api/cars";
constructor(private http : Http) { }
getCars() : Observable<Car[]> {
return this.http.get(this.apiUrl)
.map((res) => res.json())
}
}
With this code i have error: 使用此代码我有错误:
this.http.get(...).map is not a function
this.http.get(...)。map不是函数
but when i add: 但是当我添加:
import 'rxjs/add/operator/map'
Still have problem but error is: 还有问题,但错误是:
Cannot read property 'map' of undefined
无法读取未定义的属性“map”
Can you help me? 你能帮助我吗? Thanks
谢谢
As mentioned by others Http
is deprecated, use HttpClient
instead. 正如其他人所提到的,
Http
已弃用,请改用HttpClient
。 HttpClient
parses your response to an Object, so you remove the mapping of the response. HttpClient
解析您对Object的响应,因此您可以删除响应的映射。 Also, to avoid type checking errors , tell Angular what kind of response you are expecting. 另外,为避免类型检查错误 ,请告诉Angular您期望的响应类型。
So import HttpClientModule
and add it to imports array, after BrowserModule
. 因此,在
BrowserModule
之后导入HttpClientModule
并将其添加到imports数组。 In your Service import HttpClient
, inject it in your constructor and use it the following way: 在您的Service导入
HttpClient
,将其注入构造函数并以下列方式使用它:
import { Injectable } from '@angular/core';
import { Car } from "./models/car";
import { Observable } from "rxjs/Observable";
import { HttpClient } from "@angular/common/http";
import 'rxjs'
@Injectable()
export class CarsService {
private apiUrl = "http://localhost:3000/api/cars";
constructor(private httpClient : HttpClient) { }
getCars() : Observable<Car[]> {
// tell Angular you are expecting an array of 'Car'
return this.httpClient.get<Car[]>(this.apiUrl)
}
}
With angular5 HttpClient
implementation already includes inner using of the map.so it works for you automatically. 使用angular5
HttpClient
实现已经包含了map的内部使用。因此它可以自动为您服务。
just update it as 只需将其更新为
getCars() : Observable<Car[]> {
return this.http.get(this.apiUrl)
}
Also make sure you are using HttpClient
instead of Http
. 还要确保使用的是
HttpClient
而不是Http
。
Edit your code 编辑你的代码
import { Injectable } from '@angular/core';
import { Car } from "./models/car";
import { Observable } from "rxjs/Observable";
import { Http } from "@angular/http";
@Injectable()
export class CarsService {
private apiUrl = "http://localhost:3000/api/cars";
constructor(private http : Http) { }
getCars() : Observable<Car[]> {
return this.http.get(this.apiUrl)
}
}
since it automatically parse the response as JSON. 因为它会自动将响应解析为JSON。 You don't have to explicitly parse it.
您不必显式解析它。
ngOnInit() {
this.loadCars();
}
loadCars() : void {
this.carsService.getCars().subscribe((cars) => {
this.cars = cars;
this.countTotalCost();
})
}
So i move this this.countTotalCost();
所以我把这个
this.countTotalCost();
from ngOnInit
to loadCars
从
ngOnInit
到loadCars
and now .map its ok. 现在.map确定。
I have only this error: Expression has changed after it was checked. 我只有这个错误:表达式在检查后发生了变化。 Previous value: 'undefined'.
上一个值:'undefined'。 Current value: 'NaN'.
当前值:'NaN'。
<div class="row" *ngIf="grossCost"> //this line error/error context
<div class="col-sm-12">
<div class="total-cost">
<p class="text-right">TOTAL GROSS COST: {{ grossCost}} PLN</p>
</div>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.