如何使用删除方法在 angular httpclient 上修复此代码
How to fix this code on angular httpclient with delete method
(英语不是我的第一语言所以请原谅我)
嘿,我是 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 的订阅方法:
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...
});
(英语不是我的第一语言所以请原谅我) 嘿,我是 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 的订阅方法:
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...
});