相关疑难解决方法(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 5-将组件的名称实例化为字符串

我知道如何使用 ComponentFactoryResolver.resolveComponentFactory(AComponentType)

但该方法需要a Type,而在我的代码中,该类型的名称为a string

在Angular API中,有没有一种方法可以解决string?如果没有,如何Type在TypeScript中将字符串转换为?

万一以上都不可行,我将求助于地图,但它并不是那么简单,因为我需要实例化的组件将来自远程获取的UMD角度组件库。

components typescript umd angular angular5

6
推荐指数
1
解决办法
2564
查看次数

标签 统计

angular ×2

angular5 ×1

components ×1

typescript ×1

umd ×1