2017-01-09 11 views
0

Angular 2アプリケーションでは、NgModuleのプロバイダのようにロードするサービスが多く、特に特定のコンポーネントのインスタンスをチェックするサービスがあります。プロバイダがロードされたときにコンポーネントが定義されていない

if (this.appRef.components[0].instance instanceof Main) { 
    return <Main>this.appRef.components[0].instance; 
} 

SystemJSサービスをロードすると、それは、このコードをeveluates及びサービスは、各成分の前にロードされているのでMainが定義されていません。それについてのアイデア?

答えて

0

たぶん、あなたはこのような観測可能でそれを行うことができます:あなたのサービスで

export class Component implements AfterViewInit { 
    public instanceSubject: Subject<any>; 

    ngAfterViewInit() { 
     instanceSubject.next(this.instance); 
    } 

サブスクリプション:

let mainInstance; 
this.appRef.components[0].instanceSubject.asObservable().subscribe(
    (instance) => { 
     if (instance instanceof Main) { 
      mainInstance= <Main>instance; 
     } 
    } 
) 
関連する問題