当我运行服务时,代码工作正常。 在生产环境中,这部分抛出了异常
constructor(@Inject(Injector) injector: Injector, @Inject(AuthService) private authService: AuthService ) {
this.injector = injector;
}
我调试了代码,发现未定义注射器和authService。粗略地说,它既不能为AuthService也不能注入Injector类的新实例
我错过了什么吗?
在app.module中,我这样称呼TorkenInterceptor。
@NgModule({
declarations: [
AppComponent,
FullLayoutComponent,
],
imports: [
BrowserAnimationsModule,
AppRoutingModule,
SharedModule,
NgbModule.forRoot(),
StoreModule.forRoot(fromRoot.reducer.reducers),
StoreDevtoolsModule.instrument(),
EffectsModule.forRoot([])
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: fromShared.services.TokenInterceptorService,
multi: true,
},
AuthService,
AuthGuard,
......
],
bootstrap: [AppComponent]
})
export class AppModule { }
TokenInterceptor类
@Injectable()
export class TokenInterceptorService implements HttpInterceptor {
// private authService: AuthService;
injector: Injector
constructor(@Inject(Injector) injector: Injector, @Inject(AuthService) private authService: AuthService ) {
this.injector = injector;
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log('INTERCETP');
if (this.getAuthService().isAuthenticated()) {
request = request.clone({
setHeaders: {
Authorization: 'Bearer ' + this.getAuthService().getToken()
}
});
}
return next.handle(request);
}
getAuthService(): AuthService {
if (typeof this.authService === 'undefined') {
this.authService = this.injector.get(AuthService);
}
return this.authService;
}
}
环境:
操作系统: Docker
Angular版本: 7.2.4
NG命令::运行npm运行ng build---prod --output-path = dist --optimization = false