Angular Response.json()未记录 [英] Angular Response.json() not documented

查看:88
本文介绍了Angular Response.json()未记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到Response.json()方法被大量使用,而我自己也在使用它,但是我丢失了某些东西,或者Response类的文档不正确.

I see Response.json() method being used a lot, and I'm using it myself, but either I'm missing something or the documentation for the Response class is incorrect.

示例:

getCurrentTime() {
    return this._http.get('http://date.jsontest.com/')
        .map((res:Response) => res.json())
}

在Angular网站上,网址为 https://angular .io/docs/ts/latest/api/http/index/Response-class.html ,我认为该方法不是Response类的成员.

On the Angular site at https://angular.io/docs/ts/latest/api/http/index/Response-class.html, I don't see the method as a member of the Response class.

如果.json不是Response类的成员,可以有人指出我的工作原理.

If .json is not a member of the Response class, can someone point me in the direction of understanding how this works.

或者如果文档有误,请有人说.

Or if the documentation is wrong, someone please say so.

谢谢.

推荐答案

我看看 Body的源代码,您将看到json

I you look at the API Reference for Response, you'll see that Response extends Body. If you try to search for Body, you won't find it, which probably means it isn't public. If you look at the source code for Body, you'll see the code for json

/**
 * Attempts to return body as parsed `JSON` object, or raises an exception.
 */
json(): any {
  if (typeof this._body === 'string') {
    return JSON.parse(<string>this._body);
  }

  if (this._body instanceof ArrayBuffer) {
    return JSON.parse(this.text());
  }

  return this._body;
}

如果您需要说明来源,请告诉我.不过,对我来说,这看起来很不言自明.

Let me know if you need explanation of the source. It looks pretty self-explanitory to me though.

这篇关于Angular Response.json()未记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆