2016-04-22 37 views
0

我得到了一個問題。我想要一個全球服務。我發現這個:Link。 它的工作原理,但我想在初始化我的組件 事件後,我的組件被引導後調用。角度2 - 組件啓動後的事件

任何人都知道這個事件?

Lyror

更新

是的,我想這樣做:

import {GlobalService} from './GlobalService'; 
import {Component, provide, Injector, ComponentRef} from 'angular2/core'; 
import {bootstrap}  from 'angular2/platform/browser'; 
import {HTTP_PROVIDERS} from 'angular2/http'; 
import {appInjector} from './app.injector'; 
import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, 
    LocationStrategy, HashLocationStrategy} from 'angular2/router'; 
import {AppComponent}  from './app.component'; 

bootstrap(AppComponent, [ 
    ROUTER_PROVIDERS, 
    provide(LocationStrategy, { useClass: HashLocationStrategy }), 
    HTTP_PROVIDERS, 
    GlobalService 
]).then((appRef: ComponentRef) => { 

    var injector = appInjector(appRef.injector); 
    var appComp: AppComponent = injector.get(AppComponent); 

    appComp.init(); 
}); 

,但我得到兩個錯誤:

Unhandled Promise rejection: appComp.init is not a function ; Zone: ; Task: Promise.then ; Value: TypeError: appComp.init is not a function

Error: Uncaught (in promise): TypeError: appComp.init is not a function

回答

0

bootstrap函數返回一個承諾。在組件自舉時調用此級別的回調函數:

bootstrap(AppComponent).then((compRef:ComponentRef) => { 
    // Do something after the component is bootstrapped 
    var comp = compRef.instance; 
    comp.init(); 
}); 
+0

您可以提供AppComponent類的內容嗎?你有一個'init'方法嗎? –