Rest-api 调用未在 Angular 6 和 RxJS 中发送
Rest-api call is not sent in Angular 6 and RxJS
我在组件中有这个方法:
saveItem() {
return this.itemService.updateItem(this.item)
.pipe(
tap((data: any) => {
this.toastr.success('Saved');
}),
catchError((error: any) => {
return throwError(error.error);
}
)
)
}
这是 itemService 中的函数:
updateItem(item: Item) {
return this.http.post<any>(this.updateItemUrl, {
item: item
});
}
如果我在 'return this.itemService.updateItem(this.item)' 行的 saveItem() 内放置一个断点,并且在 'return this.http...' 行的 updateItem() 内放置一个断点,它会正确地进入并停止断点...
但是我有以下问题:没有触发rest-api调用,也没有发送http调用..事实上,如果我在.pipe() 运算符,它不会进入内部。为什么?
Observables(如 http-Requests)只有在至少有一个订阅者时才会激活。
=> 无订阅者 => 无 Http 请求
您可以通过
明确订阅
myObservable.subscribe()
或通过 html 模板中的异步管道隐式
<span> {{ myObservable | async }} </span>
热烈的问候
我在组件中有这个方法:
saveItem() {
return this.itemService.updateItem(this.item)
.pipe(
tap((data: any) => {
this.toastr.success('Saved');
}),
catchError((error: any) => {
return throwError(error.error);
}
)
)
}
这是 itemService 中的函数:
updateItem(item: Item) {
return this.http.post<any>(this.updateItemUrl, {
item: item
});
}
如果我在 'return this.itemService.updateItem(this.item)' 行的 saveItem() 内放置一个断点,并且在 'return this.http...' 行的 updateItem() 内放置一个断点,它会正确地进入并停止断点... 但是我有以下问题:没有触发rest-api调用,也没有发送http调用..事实上,如果我在.pipe() 运算符,它不会进入内部。为什么?
Observables(如 http-Requests)只有在至少有一个订阅者时才会激活。
=> 无订阅者 => 无 Http 请求
您可以通过
明确订阅myObservable.subscribe()
或通过 html 模板中的异步管道隐式
<span> {{ myObservable | async }} </span>
热烈的问候