简体   繁体   English

ngFor 无法读取 Object 类型的属性数据

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

相关问题 ngFor显示对象数据 - ngFor to display object data 如何使用 ngFor 按属性使用 object 对数组进行排序 - how sort array with object by property use ngFor 找不到支持 object 类型为“object”的“[object Object]”的差异。 NgFor 仅支持绑定到 Iterables,例如 Array。 json 数据 - Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Array. json data 对象样式属性值的数据类型 - Data type of object style property value Jquery偏移属性不可读错误 - Jquery offset property unreadable error JS 中具有默认属性和类类型对象的数据结构是什么? - What is this data structure with default property and type-like object in JS? ng对于不使用儿童财产? - ngFor not working with child property? 找不到支持 object 类型为“object”的“[object Object]”的差异。 NgFor 只支持绑定到 Iterables,比如 Arrays - Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables, such as Arrays Angular 7 找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Iterables,比如 Arrays - Angular 7 Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays 错误找不到类型为“对象”的其他支持对象“ [对象对象]”。 NgFor仅支持绑定到可迭代对象,例如数组 - ERROR Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM