Angular2:res.json不是函数

前端之家收集整理的这篇文章主要介绍了Angular2:res.json不是函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在我的代码中使用以下服务:

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)
            });

解决方法

为了它的价值,在Angular 5中我发现res.json不是函数错误,只需返回res,如果响应是纯json就可以修复.

猜你在找的Angularjs相关文章