是否可以引用一个模块(已经以umd或es格式编译)并在已编译的角度应用程序中动态加载它?
我尝试使用SystemJsNgModuleLoader.load加载模块,但它似乎确实适用于这种用例。
由于
编辑:同样的问题(没有答案):How to dynamically load external angular 2 module (like served from an external module.bundle.js)
答案 0 :(得分:2)
你可以这样做:
@Component({
providers: [
{
provide: NgModuleFactoryLoader,
useClass: SystemJsNgModuleLoader
}
]
})
export class ModuleLoaderComponent {
constructor(private _injector: Injector,
private loader: NgModuleFactoryLoader) {
}
ngAfterViewInit() {
this.loader.load('app/t.module#TModule').then((factory) => {
const module = factory.create(this._injector);
const r = module.componentFactoryResolver;
const cmpFactory = r.resolveComponentFactory(AComponent);
// create a component and attach it to the view
const componentRef = cmpFactory.create(this._injector);
this.container.insert(componentRef.hostView);
})
}
}
阅读Here is what you need to know about dynamic components in Angular了解更多详情。具体为Dynamic module loading and compilation
部分。