Angular 不调用subscribe的角度http更新
我的http服务中有此方法:Angular 不调用subscribe的角度http更新,angular,Angular,我的http服务中有此方法: update(field: Field) { return this.http.put<Field>(`${environment.apiUrl}/fields`, field).pipe( map((response: any) => { this.toastr.success(response.message); return response.model;
update(field: Field) {
return this.http.put<Field>(`${environment.apiUrl}/fields`, field).pipe(
map((response: any) => {
this.toastr.success(response.message);
return response.model;
}),
);
}
如果我可以这样调用它,我会更喜欢它:
this.fieldService.update(currentField).subscribe(field => console.log(field));
this.fieldService.update(currentField);
this.fieldService.update(currentField);
作为奖励,我希望两者都能做到。
这可能吗
如果我可以这样调用它,我会更喜欢它:
this.fieldService.update(currentField).subscribe(field => console.log(field));
this.fieldService.update(currentField);
this.fieldService.update(currentField);
然后从服务中调用subscribe()。但这是一个糟糕的想法,因为您通常希望能够知道更新是否成功,以及何时成功,以便链接其他操作,例如导航到显示您刚刚更新的值的页面
作为奖励,我希望两者都能做到。这可能吗
不,除非您定义两个不同的方法,或者除非您将sme参数传递给该方法,告诉它是否订阅。事实上,我假设您正在尝试为REST请求实现一个通用烤面包机。如果是这样的话,你可能会更乐意改变做这件事的方式
我个人将为此实施一项服务,您可以随时随地调用该服务,当然要尊重应用程序的模块hyerarchy。我想我会接受它:)