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

查看:15
本文介绍了如何制作角度模块以忽略核心模块中添加的 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.

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

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