这就是我现在面临的错误. (错误错误:NG02200:找不到类型为‘Object’的不同支持对象‘[Object Object]’.NgFor仅支持绑定到迭代对象,如array.您是否打算使用KeyValue管道?)

export class StudentDataComponent {

  newdata:any;

  // list = [];

  constructor (private userAPIData:StudentDataService){}

  ngOnInit(): void {
    this.userAPIData.getdata().subscribe(res =>{
      this.newdata= res;
      console.log(this.newdata)
  })
  }
  
}
<tr *ngFor="let user of newdata;">
        <td>{{user.id}}</td>
        <td>{{user.firstName}}</td>
        <td>{{user.lastName}}</td>
        <td>{{user.id}}</td>
 </tr>

推荐答案

Newdata是一个对象数据 struct ,与*ng不兼容.如果要使用其中的用户数组,请将代码更改为下面!

export class StudentDataComponent {

  newdata:any = { users: [] };

  // list = [];

  constructor (private userAPIData:StudentDataService){}

  ngOnInit(): void {
    this.userAPIData.getdata().subscribe(res =>{
      this.newdata= res;
      console.log(this.newdata)
  })

}

<tr *ngFor="let user of newdata.users;">
        <td>{{user.id}}</td>
        <td>{{user.firstName}}</td>
        <td>{{user.lastName}}</td>
        <td>{{user.id}}</td>
 </tr>

Typescript相关问答推荐

根据另一个成员推断类成员的类型

在React中实现具有独立页面的嵌套路径的最佳实践是什么?

对数组或REST参数中的多个函数的S参数进行类型判断

向NextAuth会话添加除名称、图像和ID之外的更多详细信息

为什么在回调函数的参数中不使用类型保护?

从子类构造函数推断父类泛型类型

Angular 17子路由

使用TypeScrip的类型继承

在打字脚本中对可迭代对象进行可变压缩

如何从一个泛型类型推断多个类型

如何使用TypeScrip在EXPO路由链路组件中使用动态路由?

KeyOf关键字在特定示例中是如何工作的

为什么TypeScrip假定{...省略类型,缺少类型,...选取类型,添加&>}为省略类型,缺少类型?

递归生成常量树形 struct 的键控类型

如何使用angular15取消选中位于其他组件中的复选框

如何键入';v型';

我可以在 Typescript 中重用函数声明吗?

基于可选参数的动态类型泛型类型

如何判断路由或其嵌套路由是否处于活动状态?

是否有解释为什么默认情况下在泛型类型上访问的属性不是索引访问