I'm curious about the usage note at the bottom of the Angular docs on HttpInterceptors, it states:
To use the same instance of HttpInterceptors for the entire app, import the HttpClientModule only in your AppModule, and add the interceptors to the root application injector . If you import HttpClientModule multiple times across different modules (for example, in lazy loading modules), each import creates a new copy of the HttpClientModule, which overwrites the interceptors provided in the root module.
I don't understand how to do this. How can I get the root application injector and add the HttpInterceptor of my lazy loaded modules to it?
It seems like How to import Angular HTTP interceptor only for Child module is also trying to answer this question but the answer there is equally unclear to me.
Why does this not work, and how to properly add the second lazy loaded interceptor?
AppModule
@NgModule({
imports: [RouterModule.forRoot(routes)], // routes contains a lazy load module
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptorService,
multi: true,
}
]
})
export class AppModule { }
Lazy loaded child module
@NgModule({
imports: [
LazyRoutingModule
],
declarations: [ChildComponent],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: CacheInterceptorService,
multi: true,
}
]
})
export class LazyModule { }
providedIn: 'root'
, I read somewhere that Angular automatically lazy loads them that way. – After