【发布时间】:2020-04-09 03:18:06
【问题描述】:
我正在从 API 返回数据,并且我已经放入了几个 console.log() 语句进行调试。在ngOnInit() 中,console.log 正在打印undefined,但在单独的函数中,console.log 返回正确的数据,理论上,两者之间不会进行其他处理。
ngOnInit() {
this.loadAnimalList('fur');
console.log(this.animalsFur);
}
loadAnimalList(classification: string) {
this.animalService.getAnimals(classification).subscribe((animals: Animal[]) => {
switch (classification) {
case 'fur':
this.animalsFur = animals;
break;
case 'feather':
this.animalsFeather = animals;
break;
case 'scales':
this.animalsScales = animals;
break;
case 'other':
this.animalsOther = animals;
break;
default:
this.animalsFur = animals;
break;
}
}, error => {
this.alertify.error(error, 'Failed to Load Animals...');
});
}
console.log 我留在 id 中的那个返回未定义,如果我在切换完成后放置一个(例如),或者在 case 语句中,那么正确的数据会显示在控制台中,只是不在onInit
【问题讨论】:
标签: angular typescript angular9