嗨,我是angular的新手,我想知道基于结果数据重复GET请求的正确方法是什么。
item.service.ts
ScanItems():Observable<any>{
return this.http.get<any>(url)
}
item.component.ts
GetScannedItems(){
this.ScanSubscription = this.itemService.ScanItems()
.subscribe(
data => {
RDate = new Date(data.Date).getTime();
const NDate = new Date().getTime();
const Diff = RDate - NDate
if(Diff > (-300000)){
console.log('Valid')
}
else {
// Repeat Request Here!!!
}
},
error => console.error(error),
() => this.ScanSubscription.unsubscribe()
)
}
答案 0 :(得分:0)
您可以使用retry运算符在服务中进行操作,而不是在组件中进行处理。
GetScannedItems(){
this.ScanSubscription = this.itemService.ScanItems()
.pipe(
map(
data => {
RDate = new Date(data.Date).getTime();
const NDate = new Date().getTime();
const Diff = RDate - NDate
if(Diff > (-300000)){
return of(true);
}
else {
return Observable.throw('retry');
// Repeat Request Here!!!
}
}),
delay(500),
retry()
)
}