refer : http://stackoverflow.com/questions/39033835/angularjs2-preload-server-configuration-before-the-application-starts ( angular initialize )
直接上代码
@NgModule({ imports: [ StoogesModule, IdentityRoutingModule ], exports: [], declarations: [LoginComponent], providers: [ IdentityService, { provide: APP_INITIALIZER, useFactory: IdentityInitializeFactory, //这里不要用匿名函数, webpack 会报错 deps: [IdentityService], multi: true }, AdminAuthorizeGuard, ShopAuthorizeGuard ], }) export class IdentityModule { }
factory 返回方法, 然后返回 promise.
export function IdentityInitializeFactory (identityService: IdentityService) { return async () => { await identityService.getUserByCookies(); } }
angular2 学习笔记 ( app initialize 初始化 )
原文:http://www.cnblogs.com/keatkeat/p/6358068.html