繁体   English   中英

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

[英]res.json() is a not a function in HttpClient Angular 2

之前我使用的是Angular Http模块,而res.json()方法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()而你自己不需要这样做。 这是commit的引用:

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

请参阅角度4中的HTTP和HTTPClient之间的差异? 更多细节。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM