【发布时间】:2019-06-05 10:23:33
【问题描述】:
假设我在提供程序中有这样的功能:
getAll(): Observable<CarModel[]> {
return this.http.get<CarModel[]>(this.carUrl);
}
并且在一个组件中我有一个使用提供者这个功能的功能:
getCars() {
const that = this;
this.carService.getAll().subscribe(function(cars) {
that.factoryService.setCars(cars);
this.unsubscribe();
});
}
是否可以将其替换为使用take 运算符以避免调用unsubscribe() 的函数?
getCars() {
const that = this;
this.carService.getAll().take(1).subscribe(function(cars) {
that.factoryService.setCars(cars);
});
}
我想知道当与 Angular 的 Httpclient 方法一起使用时,这是否会产生任何意外或不需要的行为?我从来没有见过这样使用 Angular 的 Httpclient - 这就是我要问的原因。
【问题讨论】:
-
从
HttpClient订阅可观察对象时,根本没有理由调用unsubscribe。
标签: javascript angular rxjs