简体   繁体   中英

Angular 5 intercept - requests are duplicated

I am using an interceptor with HttpInterceptor in angular 5 and I have a problem with rxjs where all my http requests are duplicated.

import { Router } from '@angular/router';
import { Injectable, ApplicationRef } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/empty';
import { NgxSpinnerService } from 'ngx-spinner';
import { ErrorHandlingService } from '../../service/error-handling.service';

@Injectable()
export class ApiRequestInterceptor implements HttpInterceptor {

  private count: number = 0;

  constructor(
    private readonly spinner: NgxSpinnerService,
    private readonly router: Router,
    private readonly errorHandling: ErrorHandlingService,
    private readonly applicationRef: ApplicationRef) { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    this.count++;

    if (this.count === 1) {
      this.spinner.show();
    }

    return next.handle(req)
      .catch((err: any) => {
        this.count--;
        return Observable.throw(err);
      }).do(event => {
        if (event instanceof HttpResponse) {
          this.count--;
          if (this.count === 0) this.spinner.hide();
        }
      });
  }
}

在此处输入图片说明

As you can see, my app is making requests with httpclient with different components and services and those requests happen twice. I tried removing subscribe so it only does the do function but my spinner never stops.

Does anyone have any advice for what I should do? I think I am not using rxjs correctly but not sure what the fix is.

You are calling next.handle() twice. Just return the first one, without calling subscribe :

intercept(req: HttpRequest<any>,next: HttpHandler): Observable<HttpEvent<any>> {
    this.count++;

    if (this.count === 1) {
      this.spinner.show();
    }

    return next.handle(req)
      .catch((err: any) => {
        this.count--;
        return Observable.throw(err);
      }).do(event => {
        if (event instanceof HttpResponse) {
          this.count--;
          if (this.count === 0) setTimeout(this.spinner.hide());
        }
    });
}

Small point of advice, upgrade to angular6 to take advantage of the new rxjs and tree shaking

Please take look here for solution with spinner and counting requests.

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {        
            this.count++;
            if (this.count === 1) {
              this.spinner.show();
            }
            return next.handle(request)
                .pipe(
                    tap((event: HttpEvent<any>) => {
                        if (event instanceof HttpResponse) {
                            this.count--;
                            if (this.count === 0) setTimeout(this.spinner.hide());
                        }
                    }, (err: any) => {
                            this.count = 0;
                            setTimeout(this.spinner.hide());
                    })
                );
    }

I had the same duplicated request problem. Turns out I used angular forms and had a (click) event on the button plus a and a (ngSubmit) event on the form element. solved it by removing one of them, and debouncing the button never hurts ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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