Angular 5 - instantiate a component from its name as a string

前端 未结 2 862
北荒
北荒 2021-01-17 21:33

I know how to init components using ComponentFactoryResolver.resolveComponentFactory(AComponentType)

but the method expects a Type, while i

2条回答
  •  抹茶落季
    2021-01-17 22:14

    You can do the following:

    const classesMap = {
      AComponentType: AComponentType,
      BComponentType: BComponentType
    }
    

    And then:

    CreateDynamicComponent(typeName: string) {
        ...
        const componentFactory = ComponentFactoryResolver.resolveComponentFactory(classesMap[typeName].prototype.constructor)
        ...
    }
    

提交回复
热议问题