gpt4 book ai didi

javascript - 在 Angular 的 http 中使用 rxjs take()

转载 作者:行者123 更新时间:2023-11-29 17:39:17 25 4
gpt4 key购买 nike

<分区>

假设我在提供程序中有这样的功能:

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 - 这就是我问的原因。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com