使用RXJS SharrePlay预防多个API调用
我需要防止多个API调用某些路线。 我一直听到我应该使用shareReplay()
来防止多个API调用,尤其是在您在页面之间进入并返回同一页面时。我已经使用了shareReplay()
,但是当我查看“网络”选项卡时,它会不断提出网络请求。有什么想法吗?我在这里做了一个stackblitz链接= https://stackblitz.com/edit/ eedit/edit/angular-ivy/angular-ivy -jadosn
这是我的http.services
代码:
getWaterPokemon(): Observable<Filter[]> {
return this.http.get<Filter[]>(`${this.getApi()}/type/5`, { responseType: 'json' }).pipe(
map((clients: any) => clients.pokemon.map((client: any) => client.pokemon)),
shareReplay()
);
}
这是我的water.com.ponent
代码:
ngOnInit(): void {
this.waterPokemon$ = this.httpService.getWaterPokemon().pipe(
shareReplay()
);
}
每次您输入Water Pokemon Page时,您都会看到网络请求发生。
I need to prevent multiple api calls to certain routes.
I keep hearing that I should use shareReplay()
to prevent multiple api calls especially when you are going between pages and coming back to the same page. I've used shareReplay()
but when I look at the network tab it keeps on making the network request. Any ideas? I've made a stackblitz link here = https://stackblitz.com/edit/angular-ivy-jadosn
Here is my http.services
code:
getWaterPokemon(): Observable<Filter[]> {
return this.http.get<Filter[]>(`${this.getApi()}/type/5`, { responseType: 'json' }).pipe(
map((clients: any) => clients.pokemon.map((client: any) => client.pokemon)),
shareReplay()
);
}
And here is my water.component
code:
ngOnInit(): void {
this.waterPokemon$ = this.httpService.getWaterPokemon().pipe(
shareReplay()
);
}
Everytime you enter the Water Pokemon page you'll see the Network request happening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是观察值的工作方式。
让我解释一下(这会很长,但有用)。
可观察到的是两种类型:热或冷。
可观察到的热可观察的可观察到,它在不关闭的情况下连续排放值。将其视为HTML元素上的活动听众。
可观察的可观察到的可观察到,它在第一次通话后就完成了。认为这是一个承诺。
因此,使用
http.get
是冷的,而在可观察到的shareReplay
中,您在这里做的很热,以下是:
您当前的代码,每次调用获取功能(在这种情况下是毫无用处)时,所有服务都在返回一个热观察。
您要做的就是存储获取结果,因此您不必每次都致电服务。
这个想法就是
这样,服务仅在启动数据时才刷新一次。但是,由于该服务是单身人士(除非另有制作),因此即使您更改页面也不是这样的(对于组件而言不是这种情况),只要您进出它,它就会被销毁和重新创建)。
然后,该组件只需要在服务中获取记忆数据,从而阻止您的应用程序对API进行进一步的呼叫。
This is not how the observables work.
Let me explain (it will be long but useful).
An observable is either of two types: HOT, or COLD.
A HOT observable is an observable that emits a value continuously without getting closed. Think of it like an event listener on an HTML element.
A COLD observable is an observable that gets completed after its first call. Think of it like a Promise.
So, using
http.get
is COLD, whereas usingshareReplay
on an observable makes it HOTWhat you are doing here is the following :
With your current code, all the service is doing is returning a HOT observable everytime you call the fetching function (which is useless in this case).
What you want to do is to STORE the result of the fetching, so that you don't have to call the service everytime.
The idea would be
This way, the service refreshes the data only once, when it is instanciated. BUT, since the service is a singleton (unless made otherwise), its instance lives up even when you change pages (which is not the case for a component, that gets destroyed and re-created whenever you route in/out of it).
Then, the component only has to get the memoized data in your service, preventing your app from making further calls to the API.