Jah*_*hel 22 typescript angular2-routing angular
app
|-plugins
|-plugin1
|-config.json
|-plugin1.module.ts
|-plugin1.component.ts
|-plugin2
|-config.json
|-plugin2.module.ts
|-plugin2.component.ts
Run Code Online (Sandbox Code Playgroud)
如上所示,我有"app/plugins"文件夹,其中包含插件.每个插件将包含一个"config.json"文件,它将告诉一些配置,包括 -
{
path: "feature1",
moduleFile: "feature1.module",
moduleClassName: "Feature1Module"
}
Run Code Online (Sandbox Code Playgroud)
所以我想要的是,在应用程序引导之前,它将扫描"app/plugins"文件夹并加载所有插件配置,并懒惰地注册所有模块路由.对于上面的例子,路线将是
{
path: "feature1",
loadChildren: "app/plugins/plugin1/plugin1.module#Plugin1Module"
}
Run Code Online (Sandbox Code Playgroud)
这样,我们可以将新插件放入插件文件夹并刷新应用程序,我们新删除的插件已启动并运行.
谁知道我怎么能做到这一点?
注意:我在angular2最新(2.1.0)
Maw*_*zil 14
我正在寻找与你描述的行为相同的行为,我想我已经找到了如何做到这一点,这要归功于这个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 { }
Run Code Online (Sandbox Code Playgroud)第二:这是在给出模块路径时动态加载模块的组件.
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's 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();
}
}
Run Code Online (Sandbox Code Playgroud)
}
你觉得怎么样?有帮助吗?非常感谢您的反馈.
| 归档时间: |
|
| 查看次数: |
10129 次 |
| 最近记录: |