angular2 学习笔记 ( app initialize 初始化 )

时间:2024-11-04 18:07:26

refer : http://*.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();
}
}