我是新来的!我正在尝试解析一个Angular 服务中的响应
this.httpClient.get(this.url+formula).subscribe(response => {
this.response = response
});
response应该是这样的:
Object { code: 200, formula: "2", result: 2, status: "Success" }
我得到一个错误,当我特灵得到的状态,结果或代码:代码不工作
error TS2339: Property 'code' does not exist on type 'Object'.
我将非常感谢你的帮助
2条答案
按热度按时间dz6r00yl1#
这里的问题是,typescript不能推断出您从请求中获得的是哪种对象,因此使用点表示法
myObject.someProperty
是行不通的,因为typescript不能识别someProperty。你需要显式地让typescript知道你得到的对象的类型,类似于这样:或者,您可以尝试使用括号表示法访问属性:
8yparm6h2#
我相信你必须明确地告诉Angular HttpClient,当你发出http请求时,你想访问整个响应(而不仅仅是主体)。您可以尝试以下操作:
请注意,您可以将更改为更合适的类型,即此端点应在响应体中返回的对象的类型。