角度2 - 组件启动后的事件
问题描述:
我得到了一个问题。我想要一个全球服务。我发现这个: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
答
的bootstrap
函数返回一个承诺。在组件自举时调用此级别的回调函数:
bootstrap(AppComponent).then((compRef:ComponentRef) => {
// Do something after the component is bootstrapped
var comp = compRef.instance;
comp.init();
});
您可以提供AppComponent类的内容吗?你有一个'init'方法吗? –