res.json() 不是 HttpClient Angular 2 中的函数



我之前使用过AngularHttp模块,该方法res.json()工作正常。 我最近尝试了HttpClient但随后res.json()剂量似乎不起作用.只有使用res作品才能有人告诉我 http 客户端中发生了什么变化。

return this.client.get('https://swapi.co/api/people/1/')
.map((res:Response) => {
return  res.json(); // using maps to filter data returned form the http call this json dosn't work with http client
}).map(data => {
return data; // using maps of maps to filter data returned form the map
}).flatMap((jedi) => this.http.get(jedi['homeworld'])
.map(res => {
return res.json().name; // using flat maps to combine data returned from two observables into one
}).catch((error:any) => Observable.throw(error.json().error || 'Server error')));

我切换到http客户端,因为新的拦截器可以指针受到欢迎,谢谢

是的,这是因为默认情况下,新的 http 客户端调用res.json()隐式的,您不需要自己手动调用。这是来自提交的引述:

JSON 是假定的默认值,不再需要显式解析

有关更多详细信息,请参阅 angular 4 中 HTTP 和 HTTPClient 之间的区别?

由于 HttpClient 本身添加了 res.json((,这意味着它隐式调用了这个函数,因此,显式或手动调用它是毫无价值的。所以没有必要添加这个功能。希望这有效,谢谢...

最新更新