【发布时间】:2022-01-21 09:52:33
【问题描述】:
API 模型包含不断在 API 中动态更新的 CPU 使用类别。但是当我刷新页面时,它只会更新数据,但可以在不刷新视图中的页面的情况下完成。 Setimeout 工作正常 除了 setTimeoutInterval 还有其他方法吗?
//app.service.ts
private behaviourData = new BehaviorSubject<Model>(null);
getBPmInfo(): Observable<Model>{
return this.http.get<Model>(`${this.url}`).pipe(
retry(3),
tap(data => this.behaviourData.next(data))
);
}
//app.component.ts
model!: Model;
ngOnInit() {
getInformation(){
this.bpmService.getBPmInfo().subscribe(data => {
this.model = data;
});
}
}
//app.component.html
<div>
<h1>CPU Usage{{model?.cpuUsage}}</h1>
</div>
但是数据应该动态更新而不刷新页面。尝试使用 BehaviourSubject 我不知道为什么它不起作用。谁能帮我解决这个问题。
【问题讨论】:
标签: javascript angular typescript