我有一个带有工厂的配置模块,在应用程序初始化时执行(APP_INITIALIZER)
export function ConfigFactory(config: ConfigService) {
const res = () => config.load();
return res;
}
...
{
provide: APP_INITIALIZER,
useFactory: ConfigFactory,
deps: [ConfigService],
multi: true
}
Run Code Online (Sandbox Code Playgroud)
如果我在页面/组件上打印数据,一切正常。
问题是当我尝试在从 HttpInterceptor 调用的服务中使用配置时:
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
},
Run Code Online (Sandbox Code Playgroud)
拦截器:
constructor(private authenticationService: AuthenticationService) {}
intercept(request: HttpRequest<any> , next: HttpHandler): Observable<HttpEvent<any>> {
this.authenticationService.getAccessToken().subscribe((token: string) => { this.accessToken = token.toString(); });
this.authenticationService.getCurrentUser().subscribe((currentU: string) => { this.currentUser = currentU.toString(); });
if (this.accessToken) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${this.accessToken}`,
actor: this.currentUser,
// …Run Code Online (Sandbox Code Playgroud) configuration angular-http-interceptors angular angular-lifecycle-hooks