我试图在我的代码中使用以下服务:
import {Injectable} from '@angular/core'; import {Http,Response} from "@angular/http"; import 'rxjs/Rx'; @Injectable() export class HttpService{ constructor(private http : Http){} getData(){ return this.http.get("URI") .map( (res: Response) => res.json() ); } }
问题是,在运行时它抱怨:
res.json is not a function
我已将res的数据类型定义为Response,但仍然抱怨
.map( (res: Response) => res.json() )
如果我用订阅替换地图它工作正常:
.subscribe( res =>{ res.json(); console.log("City is:"+ res.json().city.name) });