最近遇到了一些问题,想知道为什么我无法访问在 ngOnInit 函数中设置以供以后使用的对象,就像在另一个函数中一样?
我想this.appointmentDetailId
在我的cancelAppointment()
函数中访问和使用,但它是undefined
. 希望有人可以提供帮助。谢谢。
这是我的代码:
export class AppointmentDetailComponent implements OnInit {
id: any;
appointmentDetailId: any;
appointmentDetail$: Observable<AppointmentDetails>;
appointmentDetail: AppointmentDetails;
pageTitle = 'Some Default Title Maybe';
constructor(
private route: ActivatedRoute,
private title: Title,
private apiService: APIService
) {
this.appointmentDetailId = this.id;
console.log(this.appointmentDetailId);
}
ngOnInit() {
this.route.paramMap
.pipe(
tap((params: ParamMap) => {
this.id = params.get('id');
// Or this.id = +params.get('id'); to coerce to type number maybe
this.pageTitle = 'Termin Details: ' + this.id;
this.title.setTitle(this.pageTitle);
}),
switchMap(() => this.apiService.getAppointmentDetailsById(this.id))
)
.subscribe((data: AppointmentDetails) => {
this.appointmentDetail = data;
console.log(this.appointmentDetail);
});
}
cancelAppointment() {
console.log(this.appointmentDetailId);
this.apiService.cancelUserAppointment(this.appointmentDetailId);
}
}