简体   繁体   English

拦截器内部的离子警报

[英]Ionic Alert inside Interceptor

I'm intercepting network response in order to give feedback about network error.我正在拦截网络响应以提供有关网络错误的反馈。

I can't get the ionic alert module to work.我无法让离子警报模块工作。

I think that the problem is that ionic-alert use a promise which is not resolved, but I don't know what is wrong with my implementation.我认为问题在于 ionic-alert 使用了未解决的承诺,但我不知道我的实现有什么问题。 The interceptor work.拦截器工作。 I can intercept all the request.我可以拦截所有的请求。 The only problem is that I cannot see the alert poupup.唯一的问题是我看不到警报弹出窗口。

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpEvent, HttpHandler, HttpRequest, HttpErrorResponse } from '@angular/common/http';
import {Observable, throwError, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AlertController } from '@ionic/angular';


@Injectable({
  providedIn: 'root'
})
export class RespinterceptorService {

  constructor(public alertController: AlertController) { }





  async mngerror(error: HttpErrorResponse){

    let input = error.statusText+error.name;

      const alert = await this.alertController.create({
      header: 'Alert',
      subHeader: 'Subtitle',
      message: input,
      buttons: ['OK']
    });

    await alert.present();



    return throwError(error);
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {


      return next.handle(req).pipe(
        catchError(this.mngerror)
      )
  };
}

您必须在类中实现 HTTPInterceptor。

export class RespinterceptorService implements HTTPInterceptor {

Did you provide your interceptor in your app module?您是否在应用模块中提供了拦截器?

It should look like:它应该看起来像:

@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, HttpClientModule],
  providers: [
            { provide: HTTP_INTERCEPTORS, useClass: RespinterceptorService, multi: 
                true }
    ],
    bootstrap: [AppComponent]
})
export class AppModule {}

Also as mentioned before you have to implement HTTPInterceptor同样如前所述,您必须实现 HTTPInterceptor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM