Ответ 1
Я ищу такое же поведение, как тот, который вы описываете, и я думаю, что нашел, как это сделать, благодаря этой проблеме github: ленивые компоненты загрузки без маршрута
Вот код, который я написал для этого: plunker here
-
Сначала: dynamic.module.ts, динамически загруженный модуль и его компонент
import { Component, NgModule } from '@angular/core' @Component({ selector: 'my-test', template: `<h1>html template of TestComponent from DynamicModule</h1>` }) export class TestComponent { } @NgModule({ declarations: [TestComponent], exports: [TestComponent] }) export class DynamicModule { }
-
Во-вторых: вот компонент, который динамически загружает модуль, когда вы даете ему путь к модулю.
import { Component, ViewContainerRef, Compiler, ComponentFactory, ComponentFactoryResolver, ModuleWithComponentFactories, ComponentRef, ReflectiveInjector, SystemJsNgModuleLoader } from '@angular/core'; class ModuleNode { modulePath: string; componentName: string; } @Component({ moduleId: module.id, selector: 'widgetContainer', templateUrl: './widgetContainer.component.html' }) export class WidgetContainerComponent { widgetConfig: string; module: ModuleNode; cmpRef: ComponentRef<any>; constructor(private widgetService: WidgetLoader, private viewref: ViewContainerRef, private resolver: ComponentFactoryResolver, private loader: SystemJsNgModuleLoader, private compiler: Compiler){} openWebApp(menu:any) { this.loader.load(menu.modulePath) // load the module and its components .then((modFac) => { // the missing step, need to use Compiler to resolve the module embedded components this.compiler.compileModuleAndAllComponentsAsync<any>(modFac.moduleType) .then((factory: ModuleWithComponentFactories<any>) => { return factory.componentFactories.find(x => x.componentType.name === menu.componentName); }) .then(cmpFactory => { // need to instantiate the Module so we can use it as the provider for the new component let modRef = modFac.create(this.viewref.parentInjector); this.cmpRef = this.viewref.createComponent(cmpFactory, 0, modRef.injector); // done, now Module and main Component are known to NG2 }); }); } ngOnDestroy() { if (this.cmpRef) { this.cmpRef.destroy(); } }
}
Что вы думаете об этом? Помогает ли это? Большое спасибо за ваши отзывы.