为了账号安全,请及时绑定邮箱和手机立即绑定

Angular 使用全局拦截器

Angular 使用全局拦截器

繁花如伊 2021-06-07 01:29:25
我想全局使用 2 个拦截器(httpInterceptorProviders,jwtInterceptorProviders),但它在我的惰性模块中不起作用。我有 CoreModule 和 X 延迟加载模块。奇怪的是,我有一个由 swagger generator(http services) 自动生成的代码,这个调用被拦截了,但是当我使用自定义 http 服务拦截器时,它没有拦截这个请求。Index.ts 我在哪里得到提供者/** Http interceptor providers in outside-in order */export const httpInterceptorProviders = [    { provide: HTTP_INTERCEPTORS, useClass: HttpErrorInterceptor, multi: true }];export const jwtInterceptorProviders = [    { provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true }];CoreModule,我在提供者中导入我的拦截器@NgModule({  imports: [    // angular    CommonModule,    HttpClientModule,    // ngrx    StoreModule.forRoot(reducers, { metaReducers }),    StoreRouterConnectingModule.forRoot(),    EffectsModule.forRoot([AuthEffects, GoogleAnalyticsEffects]),    environment.production      ? []      : StoreDevtoolsModule.instrument({          name: "Angular NgRx Material Starter"        }),    // 3rd party    TranslateModule.forRoot({      loader: {        provide: TranslateLoader,        useFactory: HttpLoaderFactory,        deps: [HttpClient]      }    }),    ApiModule.forRoot(() => {      return new Configuration({        basePath: `${environment.HOST}:${environment.PORT}`,        apiKeys: { Authorization: "" }      });    })  ],  declarations: [],  providers: [    LocalStorageService,    AuthGuardService,    AnimationsService,    TitleService,    // retryHttpInterceptorProviders,    { provide: ErrorHandler, useClass: AppErrorHandler },    httpInterceptorProviders,    jwtInterceptorProviders,    { provide: RouterStateSerializer, useClass: CustomSerializer },    {      provide: HAMMER_LOADER,      useValue: () => new Promise(() => {})    },    AnalyticsService,    LayoutService,    StateService,    PetsServiceWithUpload  ],  exports: [TranslateModule]})
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 183 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信