从 Spring Boot 后端到 Angular 前端,每 30 秒更新一次图表值
Update charts values every 30 seconds from springboot backend to angular4 frontend
我正在做一个项目,我有一个包含大量图表和变量的仪表板,这些图表和变量需要每 30 秒更新一次。我将使用 Springboot 创建一个 API 将 return JSON 值,我将使用 Angular4 获取值并呈现我的图表。由于我既不是 Angular 也不是 Springboot 方面的专家,因此我需要您的建议,告诉我如何解决这个问题,以及什么是动态更新图表的最简单方法。我需要使用 AJAX 吗?
还有其他简单的方法吗?
谢谢大家的期待。
您应该使用 HttpClientModule,用于您对 Springboot 的请求 API。计时器功能非常简单:
export class DataService {
constructor(private http: HttpClient){}
getData() {
interval(30*1000)//just instead of writing 30000
.pipe(switchMap(() => this.http.get('your url')))
.subscribe(data => do things with data);
}
}
这是一种方法,您也可以使用 timer for example. Replace interval
with timer(0, 30*1000)
where 0 how much time to wait until the first try, and the second value is how often to repeat it. Here you can read about switchMap and interval。
切记,我是使用带管道的 RxJS 5+ 和 Angular 5+ 使用 HttpClient 编写的,旧版本的方式可能有所不同,我强烈建议使用最新版本。
我正在做一个项目,我有一个包含大量图表和变量的仪表板,这些图表和变量需要每 30 秒更新一次。我将使用 Springboot 创建一个 API 将 return JSON 值,我将使用 Angular4 获取值并呈现我的图表。由于我既不是 Angular 也不是 Springboot 方面的专家,因此我需要您的建议,告诉我如何解决这个问题,以及什么是动态更新图表的最简单方法。我需要使用 AJAX 吗? 还有其他简单的方法吗? 谢谢大家的期待。
您应该使用 HttpClientModule,用于您对 Springboot 的请求 API。计时器功能非常简单:
export class DataService {
constructor(private http: HttpClient){}
getData() {
interval(30*1000)//just instead of writing 30000
.pipe(switchMap(() => this.http.get('your url')))
.subscribe(data => do things with data);
}
}
这是一种方法,您也可以使用 timer for example. Replace interval
with timer(0, 30*1000)
where 0 how much time to wait until the first try, and the second value is how often to repeat it. Here you can read about switchMap and interval。
切记,我是使用带管道的 RxJS 5+ 和 Angular 5+ 使用 HttpClient 编写的,旧版本的方式可能有所不同,我强烈建议使用最新版本。