Angular如何结合Interval和Zip显示loading
Angular How to combine Interval with Zip to show loading
我有一个必须每分钟更新一次的页面,但我有几个 http 请求。
所以我正在尝试将 Interval 与 zip 运算符一起使用。
我如何将这两者结合起来以在所有请求后显示我的加载“isLoading”?
这是我得到的:
isLoading = true;
source = interval(60000);
ngOnInit(): void {
const responseZipado = zip(
this.monitorService.getSMPPstatus(),
this.homeService.getCampaignsToday(),
this.monitorService.getPerfomance(),
);
responseZipado.subscribe((valores) => {
this.statusSMPP = valores[0],
this.dataSourceToday = valores[1],
this.perfomanceDataSource = valores[2],
});
}
我应该使用 Zip 以外的其他操作符吗?
您可以对并行 http 请求使用 forkJoin
,因为在加载所有请求后您需要值。
示例:
interval(60000).pipe(
tap(() => this.isLoading = true),
switchMap(() => forkJoin([
this.monitorService.getSMPPstatus().pipe(catchError(error => of(error))),
this.homeService.getCampaignsToday().pipe(catchError(error => of(error))),
this.monitorService.getPerfomance().pipe(catchError(error => of(error))),
])),
).subscribe((valores) => {
this.isLoading = false;
this.statusSMPP = valores[0];
this.dataSourceToday = valores[1];
this.perfomanceDataSource = valores[2];
})
我有一个必须每分钟更新一次的页面,但我有几个 http 请求。 所以我正在尝试将 Interval 与 zip 运算符一起使用。 我如何将这两者结合起来以在所有请求后显示我的加载“isLoading”?
这是我得到的:
isLoading = true;
source = interval(60000);
ngOnInit(): void {
const responseZipado = zip(
this.monitorService.getSMPPstatus(),
this.homeService.getCampaignsToday(),
this.monitorService.getPerfomance(),
);
responseZipado.subscribe((valores) => {
this.statusSMPP = valores[0],
this.dataSourceToday = valores[1],
this.perfomanceDataSource = valores[2],
});
}
我应该使用 Zip 以外的其他操作符吗?
您可以对并行 http 请求使用 forkJoin
,因为在加载所有请求后您需要值。
示例:
interval(60000).pipe(
tap(() => this.isLoading = true),
switchMap(() => forkJoin([
this.monitorService.getSMPPstatus().pipe(catchError(error => of(error))),
this.homeService.getCampaignsToday().pipe(catchError(error => of(error))),
this.monitorService.getPerfomance().pipe(catchError(error => of(error))),
])),
).subscribe((valores) => {
this.isLoading = false;
this.statusSMPP = valores[0];
this.dataSourceToday = valores[1];
this.perfomanceDataSource = valores[2];
})