[英]property data of type Object is unreadable with ngFor
Angualr does not recognise the property data (dummy data from an API), i get the following error: Property 'data' does not exist on type 'Object' Angualr 无法识别属性数据(来自 API 的虚拟数据),我收到以下错误: “对象”类型上不存在属性“数据”
I tried removing the data property and just use "let user of users" but it gives me the following error on of : Type 'Object' is not assignable to type 'NgIterable |我尝试删除数据属性并只使用“让用户的用户”,但它给了我以下错误:类型'对象'不可分配给类型'NgIterable | null |
空 | undefined'
不明确的'
my HTML component我的 HTML 组件
<h1>Home</h1>
<ul *ngIf="users">
<li value="user" *ngFor="let user of users.data">
<img [src]="user.avatar">
<p>{{ user.first_name }} {{ user.last_name }}</p>
</li>
</ul>
the ts component : ts 组件:
import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
users: Object;
constructor(private data: DataService) {
}
ngOnInit() {
this.data.getUsers().subscribe(data => {
this.users = data
console.log(this.users)
})
}
}
the Dataservice ts component:数据服务 ts 组件:
import { Injectable } from '@angular/core';
import { HttpClient, HttpClientModule } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(public http: HttpClient) { }
getUsers() {
return this.http.get('https://reqres.in/api/users')
}
}
Just make the following changes in your ts component to compile successfully只需在您的 ts 组件中进行以下更改即可成功编译
users : any ;用户:任何;
Or或者
create the interface as the sample response.创建接口作为示例响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.