In meiner Angular RC5 App habe ich den folgenden Code, um dynamisch Komponenten zu laden:Angular 2.2 Dynamische Lastkomponenten mit System.import
public LoadComponentAsync(componentPath: string, componentName: string, locationAnchor: ViewContainerRef) {
(<any>window).System.import(componentPath)
.then(fileContents => {
return fileContents[componentName];
})
.then(component => {
this.resolver.resolveComponent(component).then(factory => {
let comp = locationAnchor.createComponent(factory, 0, locationAnchor.injector).instance;
//...
});
});
}
Eine Komponente war ich Laden sah wie folgt aus:
import { Component } from "@angular/core";
@Component({
selector: "my-test",
template: `Test`,
})
export class Main {
constructor() {
alert("Test");
}
}
Da ComponentResolver entzogen ist, suche ich nach einer neuen Lösung. Ich konnte keinen finden. Die vielversprechendste Lösung, die ich bisher gefunden:
loadSubcomponent(modulePath: string, componentName: string) {
(<any>window).System.import(modulePath)
.then((module: any) => module[componentName])
.then((type: any) => {
return this.compiler.compileModuleAndAllComponentsAsync(type)
})
.then((moduleWithFactories: ModuleWithComponentFactories<any>) => {
const factory = moduleWithFactories.componentFactories.find(x => x.componentType.name === componentName);
let componentRef = this.extensionAnchor.createComponent(factory, 0);
});
}
Quelle: Angular2 RC6 - Dynamically load component from module
Leider hat es nicht funktioniert, wie ich erwartet hatte. Wenn ich das nenne. LoadSubcomponent ("/ main", "Main"); Ich bekomme folgende Fehlermeldung:
Irgendwelche Vorschläge?