我目前正在使用以下代码在我的应用程序中动态加载角度组件.
export class WizardTabContentContainer {
@ViewChild('target', { read: ViewContainerRef }) target: any;
@Input() TabContent: any | string;
cmpRef: ComponentRef<any>;
private isViewInitialized: boolean = false;
constructor(private componentFactoryResolver: ComponentFactoryResolver, private compiler: Compiler) {
}
updateComponent() {
if (!this.isViewInitialized) {
return;
}
if (this.cmpRef) {
this.cmpRef.destroy();
}
let factory = this.componentFactoryResolver.resolveComponentFactory(this.TabContent);
this.cmpRef = this.target.createComponent(factory);
}
}
Run Code Online (Sandbox Code Playgroud)
这里resolveComponentFactory函数接受组件类型.我的问题是,有没有办法可以使用组件名称字符串加载组件,例如我将组件定义为
export class MyComponent{
}
Run Code Online (Sandbox Code Playgroud)
如何使用组件名称字符串"MyComponent"而不是类型添加上面的组件?
angular ×1