Angular 2在Bootstrap之前解析服务

前端之家收集整理的这篇文章主要介绍了Angular 2在Bootstrap之前解析服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
http://plnkr.co/edit/yhQQZxTxB8ZVFysqO6lP?p=preview

在AppComponent加载之前,如何确保我的AuthService得到解决

//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';

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

解决方法

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

//main entry point
import {bootstrap} from '@angular/platform-browser-dynamic';
import {Http,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));

猜你在找的Angularjs相关文章