Angular 加载提供程序时未定义组件
在Angular 2应用程序中,我们加载了许多服务,就像Angular 加载提供程序时未定义组件,angular,systemjs,angular2-services,Angular,Systemjs,Angular2 Services,在Angular 2应用程序中,我们加载了许多服务,就像NgModule中的提供者一样,尤其是检查某个组件实例的服务 if (this.appRef.components[0].instance instanceof Main) { return <Main>this.appRef.components[0].instance; } if(this.appRef.components[0].Main的实例instanceof){ 返回此.appRef.components[
NgModule
中的提供者一样,尤其是检查某个组件实例的服务
if (this.appRef.components[0].instance instanceof Main) {
return <Main>this.appRef.components[0].instance;
}
if(this.appRef.components[0].Main的实例instanceof){
返回此.appRef.components[0]。实例;
}
当SystemJS加载服务时,它将生成此代码,并且未定义
Main
,因为服务是在每个组件之前加载的。对此有什么想法吗?也许你可以这样使用observable:
export class Component implements AfterViewInit {
public instanceSubject: Subject<any>;
ngAfterViewInit() {
instanceSubject.next(this.instance);
}
导出类组件实现AfterViewInit{
公共实例主体:主体;
ngAfterViewInit(){
instanceSubject.next(this.instance);
}
订阅您的服务:
let mainInstance;
this.appRef.components[0].instanceSubject.asObservable().subscribe(
(instance) => {
if (instance instanceof Main) {
mainInstance= <Main>instance;
}
}
)
let main实例;
this.appRef.components[0].instanceSubject.asObservable().subscribe(
(实例)=>{
if(主实例instanceof){
mainInstance=实例;
}
}
)