you can create loading service like this:
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class LoadingService {
private isLoading = new Subject<boolean>();
constructor() {}
public start(isHideImmediately: boolean = false) {
this.isLoading.next(true);
if (isHideImmediately) {
setTimeout(() => {
this.complete()
}, 5000);
}
}
public complete() {
this.isLoading.next(false);
}
getLoadingStatus(): Observable<boolean> {
return this.isLoading.asObservable();
}
}
and each time you call api you can start loading in start loading method by default after 5s loading is closed
your code:
uploadFile() {
this.loadingService.start();
this.service.uploadFile(data).subscribe(() => {
this.toastr.success('file uploaded successfully');
this.loadingService.complete();
})
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…