Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 加载提供程序时未定义组件_Angular_Systemjs_Angular2 Services - Fatal编程技术网

Angular 加载提供程序时未定义组件

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[

在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[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=实例;
}
}
)