I am trying to create a custom HTTP interceptor that will be used to handle loading and other additional features. (Processing the load for each request manually significantly increases the amount of code).
The problem is this: The loader is activated for each request, but loading.dismiss() does not work (loading spinner continues to be active, no errors)
My configuration:
http interceptor:
@Injectable() export class MyHttpWrapper extends Http { private loading: any; constructor(connectionBackend: ConnectionBackend, requestOptions: RequestOptions,private loadingCtrl: LoadingController) { super(connectionBackend, requestOptions); } public get(url: string, options?: RequestOptionsArgs): Observable<Response> { this.showLoader(); return super.get(url, this.getRequestOptionArgs(options)) .finally<Response>(() => { this.hideLoader(); }); } public post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> { return super.post(url, body, options); } public put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> { return super.put(url, body, options); } public delete(url: string, options?: RequestOptionsArgs): Observable<Response> { return super.delete(url, options); } private getRequestOptionArgs(options?: RequestOptionsArgs) : RequestOptionsArgs { if (options == null) { options = new RequestOptions(); } if (options.headers == null) { options.headers = new Headers(); } options.headers.append('Content-Type', 'application/json'); return options; } private showLoader() { if(!this.loading){ this.loading = this.loadingCtrl.create({ dismissOnPageChange: true }); } this.loading.present(); console.log('show loader') } private hideLoader() { console.log('hide loader') console.log(this.loading) this.loading.dismiss(); } }
app.module.ts
export function httpInterceptorFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions, loadingCtrl: LoadingController) { return new MyHttpWrapper(xhrBackend, requestOptions, loadingCtrl); } @NgModule({ declarations: [ MyApp ], imports: [ BrowserModule, HttpModule, IonicModule.forRoot(MyApp), IonicStorageModule.forRoot() ], bootstrap: [IonicApp], entryComponents: [ MyApp ], providers: [ StatusBar, SplashScreen, {provide: ErrorHandler, useClass: IonicErrorHandler}, {provide: APP_CONFIG, useValue: AppConfig}, { provide: Http, useFactory: httpInterceptorFactory, deps: [XHRBackend, RequestOptions, LoadingController] } ] }) export class AppModule {}
UPDATE:
tried to add a simple service (and use it in MyHttpWrapper ), nothing changes, the same problem.
@Injectable() export class LoadingService { private loading:any; constructor(private loadingCtrl: LoadingController) { } show() { if(!this.loading){ this.loading = this.loadingCtrl.create({ dismissOnPageChange: true }); } this.loading.present(); } hide() { if (this.loading) { this.loading.dismiss(); } } }