如何使Angular模块忽略核心模块中添加的http拦截器 [英] How to make an angular module to ignore http interceptor added in a core module

查看:118
本文介绍了如何使Angular模块忽略核心模块中添加的http拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实有一个带有HttpInterceptor的核心模块,用于授权处理,并且我将此模块包含在AppModule中,这样,所有其他使用HttpClient的模块都在使用此拦截器.

I do have a core module with an HttpInterceptor for authorization handling and I include this module in AppModule, in this way all the other modules that use HttpClient are using this interceptor.

@NgModule({
  imports: [],
  declarations: [],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: AuthInterceptor,
      multi: true,
    },
  ]
})
export class CoreModule { }

如何使模块绕过默认拦截器?

How to make a module bypass the default interceptor?

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: components,
  providers: [CustomService],
  exports: components,
})
export class ModuleWithoutInterceptorModule { }

推荐答案

您可以使用HttpBackend.

You can use HttpBackend.

示例:

import { HttpClient, ..., HttpBackend } from '@angular/common/http';

@Injectable()
export class TestService {

  private httpClient: HttpClient;

  constructor( handler: HttpBackend) { 
     this.httpClient = new HttpClient(handler);
  }
....

通过这种方式,AuthInterceptor不会拦截该服务.

In this way the service is not intercepted by AuthInterceptor.

这篇关于如何使Angular模块忽略核心模块中添加的http拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆