(英语不是我的第一语言,所以请原谅我)嘿,我是Angular的新手,我正在尝试做一个http请求,当我点击一个按钮(例如)时删除医生,并且知道我在挣扎什么我必须做的是让我的代码工作。谢谢
这是doctor.service.ts
getDoctor(doctorId: string): Observable<Doctor> {
return this.http.get<Doctor>(`${this.apiUrl}/${doctorId}`, {headers})
.pipe(map((doctor: Doctor) => {
return doctor;
}), catchError((error: any) => {
this.getError(error);
return of<Doctor>(EMPTY_DOCTOR);
}));
}
deleteDoctor(DoctorId: string): Observable<void> {
return this.http.delete<void>(`${this.apiUrl}/${DoctorId}`, {headers})
}
如果你想使用你的deleteDoctor()
方法,你应该调用你的observable的subscribe方法:
deleteDoctor(DoctorId: string): Observable<void> {
this.http.delete<void>(`${this.apiUrl}/${DoctorId}`, {headers})
.subscribe(response => {
// Do something here if needed...
});
}
您也可以保留您的方法并以这种方式调用它:
this.deleteDoctor().subscribe(response => {
// Do something here if needed...
});