代码的简化版本:
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
constructor(
private readonly router: Router,
private readonly activatedRouteSnapshot: ActivatedRouteSnapshot,
@Inject(AuthServiceFactory) private readonly authServiceFactory: ServiceFactoryBase<IAuthService>,
@Inject(LoggingServiceFactory) private readonly loggingServiceFactory: ServiceFactoryBase<ILoggingService>) {
console.log('router', router);
console.log('activated-route-snapshot', activatedRouteSnapshot);
}
这些都无法解决。它失败并显示以下标准消息:
StaticInjectorError(AppModule)[InjectionToken HTTP_INTERCEPTORS -> ActivatedRouteSnapshot]:StaticInjectorError(平台:核心)[InjectionToken HTTP_INTERCEPTORS -> ActivatedRouteSnapshot]:NullInjectorError:没有ActivatedRouteSnapshot的提供者!
RouterModule
..导入应用程序的正确方法是什么?
PS我得到SharedModule
而不是AppModule
导出所有我的东西,但不是Angular的:
@NgModule({
declarations: any.concat(pipes),
providers: any
.concat(serviceFactories)
.concat(guards)
.concat(otherProviders)
.concat([{
provide: HTTP_INTERCEPTORS,
useClass: JwtInterceptor,
multi: true
}]),
exports: any.concat(pipes)
})
export class SharedModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SharedModule,
providers: any
.concat(serviceFactories)
.concat(guards)
.concat(otherProviders)
};
}
}
的AppModule
:
@NgModule({
declarations: appComponents.concat(auxComponents),
imports: [
// theirs
BrowserModule,
HttpClientModule,
AppRoutingModule,
// ours
SharedModule,
CoursesModule
],
bootstrap: [
AppComponent
]
})
export class AppModule { }