Angular 类型对象不可分配给任何类型[]
我使用的是Priming数据表。我在Angular中使用httpClient从JSON占位符中获取一些模拟数据。它在我的控制台中显示为一个对象数组,但是Visual Studio代码错误表明它是一个对象。错误为“类型对象不可分配给任何[]”。这里有什么问题 表-layout.component.tsAngular 类型对象不可分配给任何类型[],angular,primeng,Angular,Primeng,我使用的是Priming数据表。我在Angular中使用httpClient从JSON占位符中获取一些模拟数据。它在我的控制台中显示为一个对象数组,但是Visual Studio代码错误表明它是一个对象。错误为“类型对象不可分配给任何[]”。这里有什么问题 表-layout.component.ts import { BrowserModule } from '@angular/platform-browser' import { Component, OnInit, NgModule } fr
import { BrowserModule } from '@angular/platform-browser'
import { Component, OnInit, NgModule } from '@angular/core';
import { HttpClient } from '@angular/common/http'
@Component({
selector: 'app-table-layout',
templateUrl: './table-layout.component.html',
styleUrls: ['./table-layout.component.css']
})
export class TableLayoutComponent implements OnInit {
ROOT_URL: string = 'https://jsonplaceholder.typicode.com/users'
results: any[]
constructor(private http: HttpClient) { }
ngOnInit() {
this.getData();
}
getData() {
this.http.get(this.ROOT_URL).subscribe(data => {
this.results = data
console.log(this.results) //this is an array in the console
})
}
}
table-layout.component.html
<p-dataTable [value]="results">
</p-dataTable>
如果未指定从http请求返回的类型,http客户端将假定其为
对象。这会导致您看到的类型不匹配错误。您正在尝试将类型对象
分配给类型任何[]
。您可以通过执行以下操作来指定返回类型
this.http.get<any[]>(this.ROOT_URL).subscribe(...);
this.http.get(this.ROOT_URL).subscribe(…);
如果未指定从http请求返回的类型,http客户端将假定其为对象。这会导致您看到的类型不匹配错误。您正在尝试将类型对象
分配给类型任何[]
。您可以通过执行以下操作来指定返回类型
this.http.get<any[]>(this.ROOT_URL).subscribe(...);
this.http.get(this.ROOT_URL).subscribe(…);
返回this.http.get(this.url)
使用任何服务中的返回this.http.get(this.url)
使用任何服务中的显示getData()代码。可能是因为您正在返回另一种类型的数据。请显示getData()代码。可能是因为您返回了另一种类型的数据。非常感谢!非常感谢你!