我有一个仪表板应用程序,在这里我懒惰地加载小部件(与路线无关)。
我通过定义一个对象来做到这一点{name: string, loadChildren: string}。然后app.module我会做的provideRoutes(...)。
这将导致cli为每个小部件模块创建一个块。
然后在运行时,我将使用SystemJsModuleLoader加载该字符串并获取一个NgModuleRef。
使用,我可以创建一个从模块和调用组件createComponent上ViewContainerRef。
这是该函数:
loadWidget(
name: string,
container: ViewContainerRef,
widget: Widget
): Promise<{ instance: WidgetComponent; personlize?: { comp: any; factory: ComponentFactoryResolver } }> {
if (this.lazyWidgets.hasOwnProperty(name)) {
return this.loader.load(this.lazyWidgets[name]).then((moduleFactory: NgModuleFactory<any>) => {
const entryComponent = (<any>moduleFactory.moduleType).entry;
const moduleRef = moduleFactory.create(this.injector);
const compFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(entryComponent);
const comp = container.createComponent(compFactory);
(<WidgetComponent>comp.instance).widget = widget;
const personalize = (<any>moduleFactory.moduleType).personalize;
if (personalize) {
return {
instance: …Run Code Online (Sandbox Code Playgroud)