我正在使用App_Initializer调用一项服务。在我的app.module.ts提供程序部分,我有2个如下的App_Initializers:
// app.module.ts
{
provide: APP_INITIALIZER,
useFactory: (config: AppConfig) => () => config.load(),
deps: [AppConfig],
multi: true
},
{
provide: APP_INITIALIZER,
useFactory: (config: AntiforgeryService) => () => config.Load(),
deps: [AntiforgeryService, GlobalService],
multi: true
}
// Load方法在AntoforgeryService中如下所示
Load(): Promise<any> {
var date = new Date();
var sourceDate = date.getDate() + "*" + date.getDay() + "%" + date.getFullYear();
return new Promise((resolve, reject) => {
this.http.get(this.forgeryService + '/CMMService-service/GetAntiForgeryToken', { headers: new HttpHeaders().set('X-XSRF-KEY', 'GCMT-' + sourceDate) })
.map(res => res).catch((error: any) => {
return Observable.throw(error.json().error || 'TOKEN error');
}).subscribe((configResponse: any) => {
this.parseTokenData(configResponse);
resolve(true);
});
});
}
parseTokenData(data: any) {
debugger;
if (data!=null)
{
var result = JSON.parse(data.value);
this.cookieToken = result.CookieToken;
this.headerToken = result.HeaderToken;
this.globalService.cookieToken = this.cookieToken;
this.globalService.headerToken = this.headerToken;
}
}
当我从app.module.ts中的提供者中删除以下部分时,则没有问题:
{
provide: APP_INITIALIZER,
useFactory: (config: AntiforgeryService) => () => config.Load(),
deps: [AntiforgeryService, GlobalService],
multi: true
}
在我的AntiforgeryService中,我还使用了名为GlobalService的服务之一。
我也在使用AuthGuard,找到下面的AuthGuard代码:
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
import { AuthService } from './auth.service';
import { GlobalService } from '../services/GlobalService';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private globalService: GlobalService) {
}
canActivate(): boolean {
debugger;
if (window.location.href.indexOf("localhost") == -1) {
return true;
}
return this.authService.handleAuthentication();
}
}
在AuthGuard中,我们具有handleAuthentication函数,在此函数中,我们使用一个配置文件来获取ESO详细信息,如下所示:
public load() {
return new Promise((resolve, reject) => {
this.http.get('../config/config.json')
.map(res => res).catch((error: any) => {
return Observable.throw(error.json().error || 'Server error');
}).subscribe((configResponse: any) => {
debugger;
this.config = configResponse;
resolve(true);
});
});
}
有人可以建议我怎么做?