![](/img/trans.png)
[英]React / TypeScript - Type '{ index:number}' is missing the following properties from type 'IList': length, pop, push, concat, and 26 more
[英]Type 'Object' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more.ts
我在 Visual Studio 代码中遇到错误 ->
类型 'Object' 缺少类型 'any[]' 的以下属性:length、pop、push、concat 和 26 more.ts
我不知道原因,但请检查我的代码:
trainingPlanResponse: any[] = [];
this.service.postToGetData(model).subscribe(
data => {
// this.trainingPlanResponse.push(data);
this.trainingPlanResponse = data; // HERE IS ERROR!!!
},
err => {
console.log(err)
}
)
当我设置为
trainingPlanResponse: any;
这项工作,但我需要在 html 中设置
<div *ngIf="trainingPlanResponse.length > 0">
response
</div>
重要提示:我没有用于此的模型界面!
来自 http 请求的响应返回Object
类型的 Observable
例子
假设您有一个函数postToGetData()
postToGetData() {
return this.httpClient.get('my-url')
}
Typescript
将推断函数postToGetData
返回一个Observable<Object>
最简单的解决方案是简单地使用类型转换如下
postToGetData() {
return this.httpClient.get<any[]>('my-url')
}
以上将推断postToGetData
返回any[]
您还可以将函数的返回类型定义为any[]
postToGetData(): any[] {
return this.httpClient.get('my-url').pipe(map(items => items as any[]))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.