相关疑难解决方法(0)

如何使用angular2中的组件名称动态加载组件?

我目前正在使用以下代码在我的应用程序中动态加载角度组件.

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

16
推荐指数
2
解决办法
1万
查看次数

标签 统计

angular ×1