我有一个名为 Condition 的类:
export class Condition{
name: string;
date: string;
link: string;
}
还有一个名为 search 的函数,它发出 http get 请求:
public search(params: string): Observable<json[]> {
let queryString = this.serverUrl + params;
return this.http.get(queryString)
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
然后我得到一个对象,从中获取具有特定属性的条件。它的 entry 属性中绝对有一个对象数组:
this.search(`Condition?patient=Patient/107795`).subscribe(data => {
var conditionArray = data.entry; // data.entry is absolutely an ARRAY
for(var condition of conditionArray){
conditions.push({ //conditions is a Condition[]
name: condition.resource.code.coding[0].display,
date: condition.resource.dateRecorded,
link: "condition/" + condition.resource.id
});
}
} , err => { console.log(err); } );
在我编写这些代码之前,我使用启动脚本启动了这些项目:
"scripts": {
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
并且该项目按我想要的方式运行,并且控制台上没有错误。但是,当我停止它并使用启动脚本重新启动时,它给出了这个错误:
error TS2339: Property 'entry' does not exist on type 'any[]'.
您知道它出了什么问题吗?我该如何解决?
最佳答案
您的search
方法应该返回一个对象,而不是数组。声明如下:
public search(params: string): Observable<any> {
除非您有描述结果的类型,否则请使用 any
,如果有,您可以改用它。
关于javascript - 类型 'any[]' (JSON) 上不存在 Angular2 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39364080/