[英]Type 'Object' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more.ts
I got error in visual studio code ->我在 Visual Studio 代码中遇到错误 ->
Type 'Object' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more.ts类型 'Object' 缺少类型 'any[]' 的以下属性:length、pop、push、concat 和 26 more.ts
I dont know reaseon but check my code:我不知道原因,但请检查我的代码:
trainingPlanResponse: any[] = [];
this.service.postToGetData(model).subscribe(
data => {
// this.trainingPlanResponse.push(data);
this.trainingPlanResponse = data; // HERE IS ERROR!!!
},
err => {
console.log(err)
}
)
When i set to当我设置为
trainingPlanResponse: any;
This work but I need to set in html这项工作,但我需要在 html 中设置
<div *ngIf="trainingPlanResponse.length > 0">
response
</div>
Important: I don't have model interface for this!重要提示:我没有用于此的模型界面!
A response from an http request returns an Observable of type Object
来自 http 请求的响应返回Object
类型的 Observable
Example例子
Lets say you have a function postToGetData()
假设您有一个函数postToGetData()
postToGetData() {
return this.httpClient.get('my-url')
}
Typescript
will infer the function postToGetData
to return an Observable<Object>
Typescript
将推断函数postToGetData
返回一个Observable<Object>
The easiest solution to this is to simply use type casting like below最简单的解决方案是简单地使用类型转换如下
postToGetData() {
return this.httpClient.get<any[]>('my-url')
}
The above will infer postToGetData
to return any[]
以上将推断postToGetData
返回any[]
You may also define the return type of the function as any[]
您还可以将函数的返回类型定义为any[]
postToGetData(): any[] {
return this.httpClient.get('my-url').pipe(map(items => items as any[]))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.