gfe*_*els 2 javascript rxjs angular
假设我在提供者中有一个这样的函数:
getAll(): Observable<CarModel[]> {
return this.http.get<CarModel[]>(this.carUrl);
}
Run Code Online (Sandbox Code Playgroud)
在组件中,我有一个使用提供程序的此函数的函数:
getCars() {
const that = this;
this.carService.getAll().subscribe(function(cars) {
that.factoryService.setCars(cars);
this.unsubscribe();
});
}
Run Code Online (Sandbox Code Playgroud)
是否可以将其替换为使用take运算符的函数以避免调用unsubscribe()?
getCars() {
const that = this;
this.carService.getAll().take(1).subscribe(function(cars) {
that.factoryService.setCars(cars);
});
}
Run Code Online (Sandbox Code Playgroud)
我想知道与 Angular 的 Httpclient 方法一起使用时是否会产生任何意外或不需要的行为?我从来没有见过像这样使用 Angular 的 Httpclient - 这就是我问的原因。
complete请求完成后,HttpClient 会调用流。调用完整信号的 Observable 表示不会再发出任何值,因此任何订阅者都不会再收到任何值。这意味着没有理由取消订阅或使用takehttp 请求。
getAll(): Observable<CarModel[]> {
return this.http.get<CarModel[]>(this.carUrl); // <- Emits once and completes
}
Run Code Online (Sandbox Code Playgroud)
从文档中查看此部分。笔记:
来自 HttpClient 的可观察对象始终发出单个值,然后完成,不再发出。
| 归档时间: |
|
| 查看次数: |
1443 次 |
| 最近记录: |