【发布时间】:2021-08-26 13:10:47
【问题描述】:
我的组件需要在发出 API 请求之前检查是否设置了某些应用首选项。现在我已经这样设置了,我在 2 分钟的计时器上更新我的组件数据:
ngOnInit(): void {
this.subscription = timer(0, 120 * 1000).subscribe(() => {
this.shopService.getPreferencesAsObservable().subscribe(preferences => {
if(preferences) {
this.getInitialPendingSlotsOrders();
this.getInitialPendingNoSlotsOrders();
}
});
});
}
getInitialPendingSlotsOrders(){
this.apiService.fetchShopOrders("status=PENDING&only_slots=true").subscribe(orders=> {
/* do stuff with orders */
/* it can happen that I need to get another page of data */
if(orders.next_page){
this.getNextSlotsSetOfPendingOrders();
}
});
}
getInitialPendingNoSlotsOrders(){
this.apiService.fetchShopOrders("status=PENDING").subscribe(orders=> {
/* do stuff with orders */
/* it can happen that I need to get another page of data */
if(orders.next_page){
this.getNextNoSlotsSetOfPendingOrders();
}
});
}
getNextSlotsSetOfPendingOrders() {
this.apiService.fetchNextSetOfOrders(this.nextSlotsUrl).subscribe(nextSetOfOrders => {
/* do stuff with next set of orders */
})
}
getNextNoSlotsSetOfPendingOrders() {
this.apiService.fetchNextSetOfOrders(this.nextNoSlotsUrl).subscribe(nextSetOfOrders => {
/* do stuff with next set of orders */
})
}
我认为这会起作用,但我遇到了一种情况,我看到正在进行一些额外的 API 调用。我知道这与链接可观察对象有关。我可以做些什么来清理它?
提前谢谢你。
【问题讨论】:
标签: angular typescript rxjs