代码之家  ›  专栏  ›  技术社区  ›  bharath muppa

角度4.2。x拦截器未被执行

  •  1
  • bharath muppa  · 技术社区  · 7 年前


    -角度4.2.4

    我正试图使用以下角度代码拦截服务

    import {Injectable} from '@angular/core';
    import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from 
    '@angular/common/http';
    import { Observable } from 'rxjs/Rx';
    export class LocalHttpClientInterceptor implements HttpInterceptor {
    intercept(req: HttpRequest<any>, next: HttpHandler): 
    Observable<HttpEvent<any>> {
      const username = 'me';
      const password = 'blahblah';
      const authReq = req.clone({headers: req.headers.set('Authorization', 
    'Basic ' + btoa(username + ':' + password))});
      return next.handle(authReq);
      }
     }
    

    import { LocalHttpClientInterceptor } from './local-http-client-
    interceptor';
      @NgModule({
       imports: [
       CommonModule,
       FormsModule,
       ReactiveFormsModule,
       MdAutocompleteModule,
       MdInputModule,
       MdIconModule,
       HttpModule,
       MdGridListModule,
       FlexLayoutModule
     ],
     declarations: [ UserSearchComponent, ObjectComponent],
     exports: [UserSearchComponent, ObjectComponent, FlexLayoutModule],
     providers : [{
     provide: HTTP_INTERCEPTORS,
     useClass: LocalHttpClientInterceptor,
     multi: true,
     }, UserSearchService, LocalHttpClientService, StorageService]
    })
     export class SharedModule { }
    

    我可以看到intercept函数定义处的调试点被触发,但在完成一些服务调用后,intercept内部的代码并没有被调用。

    我使用@angular/http中的angular-http调用服务,使用get、options和post方法

    1 回复  |  直到 7 年前
        1
  •  2
  •   Arun Redhu    7 年前

    根据官方的变更日志,包裹 @angular/common/http 可从以下网址获得 angular 4.3.0 4.2.4 @角度/公共/http 而不是 @angular/http .

    了解如何使用新 httpClient 角度 https://angular.io/guide/http

    https://plnkr.co/edit/wtl8EIaCpov6ScwBfPWX?p=preview

    有关更多更改日志,请访问 https://github.com/angular/angular/blob/master/CHANGELOG.md

    推荐文章