【发布时间】:2020-08-05 07:15:09
【问题描述】:
我创建了一个带有@Input() 值的组件,但我总是得到undefind。
这是我的代码:
父.html
<added-item
[minItemValueLength]="minItemValueLength"
[maxItemValueLength]="maxItemValueLength"
[listOfItems]="listOfItems"
></added-item>
children.ts
export class AddedItemComponent implements OnInit, OnChanges{
@Input() minItemValueLength: number;
@Input() maxItemValueLength: number;
@Input() listOfItems: string[];
ngOnInit() {
console.log(this.minItemValueLength) // return 3
console.log(this.listOfItems) // return undefined
}
}
父.ts
export class LocationsTabComponent implements OnInit {
listOfItems: string[];
minItemValueLength = 3;
maxItemValueLength = 15;
public ngOnInit(): void {
this.locationService.getLocations().subscribe((locations) => {
this.getLocationsList();
console.log(this.listOfItems) // return Array(6)
});
}
private getLocationsList() {
this.listOfItems = this.locations.map((location: Location) => location.name);
console.log(this.listOfItems) // return Array(6)
}
}
如果我在儿童中使用ngOnChanges(),那么它首先输出我:
data undefined 然后:
data Array(6)
如何在onInit 中获取此值?
非常感谢您的帮助!
【问题讨论】: