Angular - HTTP 拦截器和配置加载于 APP_INITIALIZE
Angular - HTTP Interceptor And Configuration loaded on APP_INITIALIZE
我有一个带有工厂的配置模块,它在应用程序初始化时执行 (APP_INITIALIZER)
export function ConfigFactory(config: ConfigService) {
const res = () => config.load();
return res;
}
...
{
provide: APP_INITIALIZER,
useFactory: ConfigFactory,
deps: [ConfigService],
multi: true
}
如果我在 page/component 上打印数据,一切正常。
问题是当我尝试在从 HttpInterceptor 调用的服务中使用配置时:
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
},
拦截器:
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,
// modulo: 'AltaActivoFijo'
}
});
}
return next.handle(request);
}
身份验证服务:
export class AuthenticationService implements AuthService {
API_URL = this.configService.tokenServer;
....
问题是 configService 未定义。
是否有任何方法可以将拦截器初始化推迟到 APP_INITIALIZER 完成?
我的目标是为所有环境创建一个独特的构建。
尝试不同的选择后:
我找不到好的、可维护的和简洁的问题解决方案。
Is there any way to postpone an interceptors initilization until
APP_INITIALIZER is finished?
见
我个人使用 deg 答案,其中简单地使用 HttpBackend 依赖而不是 APP_INITIALIZE 中的 HttpClient,因为我以前通过 http 调用检索配置。 Http Call 上升了拦截器,基本上需要配置,最终陷入循环依赖。 (HttpBackend 不唤醒拦截器)
export class AuthenticationService implements AuthService {
API_URL = this.configService.tokenServer; ....
在你的情况下,你似乎也缺少依赖性。缺少一些代码,但在 AuthenticationService 模块中,您可能应该声明依赖关系,例如:
{ provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, deps: [AuthenticationService ], multi: true },
并且变量初始化应该依赖于服务构造函数:
export class AuthenticationService implements AuthService {
API_URL = this.configService.tokenServer;
....
应该是:
export class AuthenticationService implements AuthService {
constructor(configService: ConfigService) {
this.API_URL = configService.tokenServer;
}
希望它能有所帮助,即使真的很晚
我有一个带有工厂的配置模块,它在应用程序初始化时执行 (APP_INITIALIZER)
export function ConfigFactory(config: ConfigService) {
const res = () => config.load();
return res;
}
...
{
provide: APP_INITIALIZER,
useFactory: ConfigFactory,
deps: [ConfigService],
multi: true
}
如果我在 page/component 上打印数据,一切正常。
问题是当我尝试在从 HttpInterceptor 调用的服务中使用配置时:
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
},
拦截器:
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,
// modulo: 'AltaActivoFijo'
}
});
}
return next.handle(request);
}
身份验证服务:
export class AuthenticationService implements AuthService {
API_URL = this.configService.tokenServer;
....
问题是 configService 未定义。
是否有任何方法可以将拦截器初始化推迟到 APP_INITIALIZER 完成?
我的目标是为所有环境创建一个独特的构建。
尝试不同的选择后:
我找不到好的、可维护的和简洁的问题解决方案。
Is there any way to postpone an interceptors initilization until APP_INITIALIZER is finished?
见
我个人使用 deg 答案,其中简单地使用 HttpBackend 依赖而不是 APP_INITIALIZE 中的 HttpClient,因为我以前通过 http 调用检索配置。 Http Call 上升了拦截器,基本上需要配置,最终陷入循环依赖。 (HttpBackend 不唤醒拦截器)
export class AuthenticationService implements AuthService { API_URL = this.configService.tokenServer; ....
在你的情况下,你似乎也缺少依赖性。缺少一些代码,但在 AuthenticationService 模块中,您可能应该声明依赖关系,例如:
{ provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, deps: [AuthenticationService ], multi: true },
并且变量初始化应该依赖于服务构造函数:
export class AuthenticationService implements AuthService {
API_URL = this.configService.tokenServer;
....
应该是:
export class AuthenticationService implements AuthService {
constructor(configService: ConfigService) {
this.API_URL = configService.tokenServer;
}
希望它能有所帮助,即使真的很晚