[英]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.