Angular 2在Bootstrap之前解析服务

时间:2016-07-12 01:22:38

标签: angular

http://plnkr.co/edit/yhQQZxTxB8ZVFysqO6lP?p=preview

如何确保在加载A​​ppComponent之前解析我的AuthService?

0000 61 62 63 64
0010 71 72 73 74

1 个答案:

答案 0 :(得分:0)

您可以使用APP_INITIALIZER令牌来提供您的authFactory:

//main entry point
import {bootstrap} from '@angular/platform-browser-dynamic';
import {Http,HTTP_PROVIDERS} from '@angular/http';
import {App} from './app';
import {AuthService} from './auth.service';
import {provide, enableProdMode, APP_INITIALIZER} from '@angular/core';

export function authFactory(auth: AuthService){
    return auth.login()
}

bootstrap(App, [
    HTTP_PROVIDERS,
    {
        provide: APP_INITIALIZER,
        useFactory: (auth),
        deps: [AuthService],
        multi: true
    }
]).catch(err => console.error(err));