添加{ provide: HTTP_INTERCEPTORS, useClass: HttpsInterceptorService, multi: true }到@NgModule.decorators的providers数组中解决。在HttpsInterceptorService中实现intercept方法。代码示例:
@Injectable() export class HttpsInterceptorService implements HttpInterceptor {
constructor(private auth: AuthService) {}
intercept(
req: HttpRequestBearer ${this.auth.getToken()}
)
});
return next.handle(newReq);
}
}
@NgModule({ imports: [HttpClientModule], providers: [ AuthService, { provide: HTTP_INTERCEPTORS, useClass: HttpsInterceptorService, multi: true } ] }) export class AppModule {}