如何在Angular 6应用程序中连续命中数据库以获取数据库中的更新值。 如果需要使用rxjs计时器,如何有效地使用它来满足我的要求。
答案 0 :(得分:1)
您可以使用rxjs在angular6中使用间隔。
@Test
@UsingDataSet({"datasets/user.yml", "datasets/person-address.yml"})
@ShouldMatchDataSet("expectations/address/person-addresses-after-insert.yml")
public void postMethodShouldCreateAddress() throws Exception {
并使用方法-
import { interval } from 'rxjs';
答案 1 :(得分:0)
下面是您每隔2秒轮询一次的方法:
Observable.interval(2000).startWith
(this.getCompanyDetails(name))
.subscribe(res => {
// do whatever you want here with the response
});
getCompanyDetails正在拨打http。
答案 2 :(得分:0)
这是对后端API的简单定期调用,只需调用一次makePeriodicReq
方法(例如在ngOnInit
中):
import { Subscription, timer } from 'rxjs';
import { switchMap } from 'rxjs/operators';
subscription: Subscription;
makePeriodicReq() {
this.subscription = timer(0, 10000).pipe(
switchMap(() => this.myService.getFromApi())
)
.subscribe(resp => {
console.debug('resp', resp);
});
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
请注意,getFromApi
方法返回一个可观察值。
答案 3 :(得分:0)
interval(5000).
pipe(switchMap(() => this.myService.getFromApi()))
.subscribe(res => console.log(res));