Rah*_*gli 24 typescript angular
当我尝试将数据从父组件传递到子组件时.我在控制台中收到未定义的消息.数据采用数组的形式.
parent.component.html
<div class="section welcome-section fp-section fp-table" *ngFor="let section of sections">
<div class="fp-tableCell">
<app-question-card [data]="section"></app-question-card>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
child.component.ts
@Input() data;
question = [];
constructor() {
this.question = this.data;
}
ngOnInit() {
console.log(this.question); //returns undefined
}
Run Code Online (Sandbox Code Playgroud)
Igo*_*gor 35
你不能在构造函数中进行赋值,因为尚未填充值,它应该ngOnInit
像检查值一样完成.
@Input() data;
question = [];
constructor() {
}
ngOnInit() {
this.question = this.data;
console.log(this.question);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35055 次 |
最近记录: |