【发布时间】:2020-04-09 22:30:03
【问题描述】:
我正在尝试使用 GET 请求来接收要放入局部变量的数据。如果这在 subscibe 代码中,我已经能够获取局部变量以在控制台管理员中显示其数据。但是,当我将 console.log 放在外面时,它不起作用。为什么控制台中没有显示“this.taskSchedule”?
export class TestTaskComponent implements OnInit {
profileForm: FormGroup;
taskSchedule: any = {};
constructor(
private taskScheduleService: TaskScheduleService,
private stateStorageService: StateStorageService,
private router: Router) { }
ngOnInit() {
this.taskId = this.stateStorageService.getCurrentTaskId();
this.taskScheduleService.getTaskSchedule(19).subscribe(data => {
this.taskSchedule = data;
});
console.log(this.taskSchedule);
}
}
在控制台输出中,它只是说“未定义”。 它应该输出 API 数据
【问题讨论】: