我的项目中有父组件和子组件,以及从父组件中的 API 调用的对象数组。
- 父组件
public Questions = [];
ngOnInit(): void {
this.loadQuestions();
}
<div *ngIf="Questions ">
<app-child-card *ngFor="let item of Questions; index as i;" [data]="item" [index]="i" ></app-child-card>
</div>
- 子组件
@Input() data: any;
@Input() index: any;
ngOnInit(): void {
console.log(this.data, this.index);
}
即使我的问题数组是空的,子组件也会在每次尝试中准确呈现 3 次,并undefined undefined
在控制台中得到输出。