在Angular网站中,有一个新的api SystemJsNgModuleLoader。
剂量有人知道我如何使用此api在应用程序中加载动态模块?
我知道您会找到解决方案的,但我为其他人提供参考。该代码已得到很好的注释,并且很好理解。
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();
});
});
}
通过使用以上功能,我们可以从任何来源注入模块。有关更多信息,请参阅此。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句