我在我的项目中遇到了同样的问题。版本:“7.1.0”。
所以我的解决方案是创建一些布尔变量来加载更多项目。当您从 API 加载一些数据时,将其设置为 true - 然后设置为 false。
在你的函数中使用它的例子。
onReachEnd(): void {
if (this.isLoadingData) {
return;
}
// Call the API/function for more items here
}
因为我不知道您是否有一些服务来处理请求等。我将通过使用 BehaviorSubject 向您展示一些示例。
假设您的服务名为dataService
。我们可以在这里创建BehaviorSubject
- isLoadingData。
private isLoadingDataBehaviorSubject BehaviorSubject<boolean> = new BehaviorSubject(false);
因为它是服务,我们可以在这里创建public
observable:
isLoadingData
= this.isLoadingDataBehaviorSubject.asObservable();`
现在,当我们得到所有这些后,我们可以创建函数来调用该服务中的 API:
loadData(): Observable<any> {
this.isLoadingDataBehaviorSubject.next(true);
return this.http.get(url).pipe(
tap((response) => {
this.isLoadingDataBehaviorSubject.next(false);
return response;
}),
catchError((error: HttpErrorResponse) => {
this.isLoadingDataBehaviorSubject.next(false);
return of({});
})
);
}
你还必须在你的组件中订阅这个 isLoadingData 并设置它的值。最简单的方法,例如:
ngOnInit(): {
this.dataService.isLoadingData.subscribe(x => {
this.isLoadingData = x;
});
}