2016-09-06 4 views
4

에는 새로운 API SystemJsNgModuleLoader이 있습니다.어떻게 각도 2에서 "SystemJsNgModuleLoader"를 사용하여 동적 모듈을 앱으로 가져올 수 있습니까? 각도 웹 사이트의

아무도 내가이 API를 사용하여 앱의 동적 모듈을로드하는 방법을 알고 있습니까?

+0

참조 http://stackoverflow.com/questions/40293240/how-to-manually-lazy-load-a-module 및 http://stackoverflow.com/questions/41171593/how-to-lazy-load- angle-2-component-in-a-tabview-primeng/41178949 # 41178949 – yurzui

답변

2

당신이 해결책을 찾았을 거라는 것을 알고 있지만, 다른 사람들의 참조를 위해 나는 해결책을 제시하고 있습니다. 코드는 잘 설명되어 있으며 잘 이해할 수 있습니다.

createComponent(fileName, componentName){ 
    let injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector); 
    // 1. Create module loader 
    let loader = new SystemJsNgModuleLoader(this.compiler); 
    loader.load(fileName).then((nmf:NgModuleFactory<any>)=>{ 
     // 2. create NgModuleRef 
     let ngmRef = nmf.create(injector); 
     // 3. Create component factory 
     let cmpFactory = ngmRef.componentFactoryResolver.resolveComponentFactory(componentName); 
     // 4. Create the component 
     let componentRef = this.span.createComponent(cmpFactory,0,injector,[]); 
     // 5. Init the component name field. 
     componentRef.instance.name = "Some Name"; 
     // 6. Refresh the component area. 
     componentRef.changeDetectorRef.detectChanges(); 
     componentRef.onDestroy(()=> { 
      componentRef.changeDetectorRef.detach(); 
     }); 
    }); 
} 

위의 기능을 사용하면 모든 소스에서 모듈을 주입 할 수 있습니다. 더 자세한 내용은 this을 참조하십시오.

+0

안녕하세요, 저는 같은 문제에 직면하고 있습니다. 파일 이름의 가치가 무엇인지 물어볼 수 있습니까? –