【发布时间】:2018-07-23 19:52:42
【问题描述】:
API 中的数据不断变化。在 Angular 中刷新数据的最佳方法是什么? bitfinex.com API
我有一个服务和组件。在服务中,我使用“get”下载数据。
服务:
getPrice(){
return this.http.get('xxx');
}
组件:
ngOnInit() {
this.getPrices();
setInterval(() => {
this.getPrices();
}, 5000);
}
getPrices(){
this.tick.getPrice().subscribe(prices => {
console.log(prices);
});
}
正如您在组件中看到的,我使用“setInterval”并每 5 秒刷新一次。
我尝试直接在服务中刷新数据。
return interval(5000).pipe(
// ignore new term if same as previous term
distinctUntilChanged(),
// switch to new search observable each time the term changes
switchMap(() => this.http.get('xxx')),
);
但它仅在 5 秒后显示数据。
【问题讨论】:
-
您可以在后端和前端使用 Web 套接字吗?因为对于这个应用程序来说,它们可能比长轮询更好。