Angular 角度-在延迟加载的模块中未加载拦截器

Angular 角度-在延迟加载的模块中未加载拦截器,angular,angular-httpclient,Angular,Angular Httpclient,我创建了一个拦截器,该拦截器将令牌附加到授权头中,授权头仅用于在特性延迟加载模块中进行的API调用 但是,我不认为拦截器被称为no控制台。日志在reports模块中显示 我在其他问题上读到,这可能与HTTPClientModule有关。此HttpClientModule仅在我的主app.module中初始化过一次 如何使拦截器仅对延迟加载的功能模块工作 auth.interceptor.ts 从'@angular/core'导入{Injectable}; 从'@angular/common/ht

我创建了一个拦截器,该拦截器将令牌附加到授权头中,授权头仅用于在特性延迟加载模块中进行的API调用

但是,我不认为拦截器被称为no
控制台。日志在reports模块中显示

我在其他问题上读到,这可能与
HTTPClientModule
有关。此
HttpClientModule
仅在我的主
app.module
中初始化过一次

如何使拦截器仅对延迟加载的功能模块工作

auth.interceptor.ts

从'@angular/core'导入{Injectable};
从'@angular/common/http'导入{HttpInterceptor,HttpHandler,HttpRequest,HttpEvent,HttpResponse};
从“rxjs/Observable”导入{Observable};
导入'rxjs/add/operator/do';
从“/../services/auth/auth.service”导入{AuthService};
@可注射()
导出类AuthInterceptor实现HttpInterceptor{
构造函数(私有身份验证:身份验证服务){
log('start interceptor');
}
拦截(请求:HttpRequest,下一步:HttpHandler){
console.log(“拦截”);
const authToken=this.auth.getProfileToken();
console.log(authToken);
const authReq=req.clone({
headers:req.headers.set('Authorization',authToken)
});
返回next.handle(authReq);
}
}
报告.module.ts

从'@angular/core'导入{NgModule};
从“@angular/common”导入{CommonModule};
从“@angular/common”导入{UpperCasePipe};
从“@angular/common”导入{DatePipe};
从“@angular/common/HTTP”导入{HTTP_拦截器};
从“./reports routing.module”导入{ReportsRoutingModule};
...
从“/../shared/shared.module”导入{SharedModule};
..
从“/../core/interceptors/auth.interceptor”导入{AuthInterceptor};
@NGD模块({
进口:[
公共模块,
共享模块,
ReportsRoutingModule,
],
声明:[
...
],
入口组件:[
...
],
提供者:[日期管道,大写套管,
{提供:HTTP_拦截器,useClass:AuthInterceptor,multi:true}
]
})
导出类ReportsModule{}
应用程序模块.ts

从'@angular/platform browser'导入{BrowserModule};
从“@angular/core”导入{NgModule};
从“@angular/platform browser/animations”导入{BrowserAnimationsModule};
从“./app routing.module”导入{AppRoutingModule};
从“./app.component”导入{AppComponent};
从“./shared/shared.module”导入{SharedModule};
从“./core/core.module”导入{CoreModule};
从'@angular/common/http'导入{HttpClientModule};
@NGD模块({
声明:[
应用组件
],
进口:[
浏览器模块,
批准模块,
BrowserAnimationsModule,
HttpClientModule,
共享模块,
CoreModule.forRoot(),
],
提供者:[],
引导:[AppComponent]
})
导出类AppModule{}
应用程序路由.module.ts

从'@angular/core'导入{NgModule};
从'@angular/router'导入{Routes,RouterModule};
从“../app/core/guard/auth/auth.guard”导入{AuthGuard};
常数路由:路由=[
{
路径:“报告”,
loadChildren:“./reports/reports.module#ReportsModule”,
激活:[
AuthGuard
]
}
];
@NGD模块({
导入:[RouterModule.forRoot(路由)],
导出:[路由模块],
提供者:[AuthGuard]
})
导出类AppRoutingModule{}

当延迟加载的模块导入另一个模块时,
HTTP\u拦截器
提供者令牌被重置,该模块本身导入了
HttpClientModule

因此,
HttpClientModule
可以包含在应用程序模块中,并且只需要一次


每次模块在惰性加载模块中加载时,
HttpClient
服务的新实例被注入到未配置为使用
AppModule
中配置的拦截器的模块中。因此,在模块级而不是应用程序级添加拦截器。

将HttpClientModule导入到惰性(reports.module.ts)加载的模块中,拦截器将触发:

...
@NgModule({
    imports: [
        CommonModule,
        SharedModule,
        ReportsRoutingModule,
        HttpClientModule,
    ],
...

我建议您将拦截器移动到主模块,并将其编码为仅截获到相关域的请求。报告您是惰性加载模块吗???@jornsharpe您是否建议这是不可能的?你把它编码成只截取相关域的请求是什么意思?@PranayRana没错,你可以从app-routing.module中看到这一点。Reports是我的延迟加载功能模块。我不认为是,不。我的意思是在拦截器中检查请求并决定是否对其执行任何操作。HttpClientModule应该,并且应该只包括在大多数用例的应用程序模块中。