我看到 Response.json() 方法被大量使用,我自己也在使用它,但要么是我遗漏了什么,要么是 Response 类的文档不正确。
例子:
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 类的成员。
如果 .json 不是 Response 类的成员,有人可以指出我理解其工作原理的方向吗。
或者如果文档有误,请有人指出。
提前致谢。
最佳答案
如果您查看 API Reference for Response ,您会看到 Response
扩展了 Body
。如果您尝试搜索 Body
,您将找不到它,这可能意味着它不是公开的。如果你看the source code for Body ,您将看到 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;
}
如果您需要来源说明,请告诉我。不过,这对我来说看起来很不言自明。
关于json - Angular Response.json() 未记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309865/