我有一个由angular 6驱动的电子应用程序,我正在尝试获得类似于插件系统的东西。我会从一个已知的位置延迟加载每个插件模块,然后使用其主要组件。
我的文件夹结构:
app
plugins
> test.module.ts
plugin-holder
> plugin-holder.component.ts
这是我使用的方法:
ngOnInit(){
this.route.params.subscribe((params: Params) => {
this.id = params["id"];
this.loadPlugin({
componentName: "TestComponent",
// modulePath: "D:/jonio/Dokumente/Programmieren/1GAMES/Overlay-R/plugins/netflix/netflix.module.ts#NetflixModule"
modulePath: "./app/plugins/test.module#DynamicModule"
})
})
}
loadPlugin(plugin: Plugin){
let injector = ReflectiveInjector
.fromResolvedProviders([], this.viewref.parentInjector);
// Create module loader
let loader = new SystemJsNgModuleLoader(this.compiler);
loader.load(plugin.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 === plugin.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
});
});
}
但是我总是收到一个错误消息,那就是模块无法加载。
您知道为什么这不起作用吗?