加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 如何使用ResolveComponentFactory()但使用字符串作为

发布时间:2020-12-17 07:22:58 所属栏目:安全 来源:网络整理
导读:我想做什么: 使用类似于“resolveComponentFactory()”的东西,但使用’string’标识符来获取组件工厂. 获得后,开始利用“createComponent(Factory)”方法. Plnkr示例 – enter link description here 在该示例中,您将看到“AddItem”方法 addItem(component
我想做什么:

>使用类似于“resolveComponentFactory()”的东西,但使用’string’标识符来获取组件工厂.
>获得后,开始利用“createComponent(Factory)”方法.

Plnkr示例 – > enter link description here

在该示例中,您将看到“AddItem”方法

addItem(componentName:string):void{
    let compFactory: ComponentFactory;
    switch(componentName){
        case "image":
            compFactory = this.compFactoryResolver.resolveComponentFactory(PictureBoxWidget);
            break;
        case "text":
            compFactory = this.compFactoryResolver.resolveComponentFactory(TextBoxWidget);
            break;
}

//How can I resolve a component based on string
//so I don't need to hard cost list of possible options


this.container.createComponent(compFactory);

}

“compFactoryResolver:ComponentFactoryResolver”在构造函数中注入.

正如您将注意到的,必须对switch语句中的每个排列进行硬编码都不太理想.

当将ComponentFactoryResolver记录到控制台时,我发现它包含一个包含各种工厂的Map.

CodegenComponentFactoryResolver {_parent: AppModuleInjector,_factories: Map}

但是,这张地图是私人的,无法轻易访问(据我所知).

有没有更好的解决方案然后以某种方式滚动我自己的类来访问这个工厂列表?

我看过很多关于人们试图创建动态组件的消息.但是,这些通常是在运行时创建组件.这里讨论的组件已经预先定义,我试图使用字符串作为密钥来访问工厂.

任何建议或建议都非常感谢.

非常感谢你.

它要么定义可用组件的映射,
const compMap = {
  text: PictureBoxWidget,image: TextBoxWidget
};

或者将标识符定义为将用于生成地图的静态类属性,

const compMap = [PictureBoxWidget,TextBoxWidget]
.map(widget => [widget.id,widget])
.reduce((widgets,[id,widget]) => Object.assign(widgets,{ [id]: widget }),{});

然后使用地图

let compFactory: ComponentFactory;

if (componentName in compMap) {
    compFactory = this.compFactoryResolver.resolveComponentFactory(compMap[componentName]);
} else {
    throw new Error(`Unknown ${componentName} component`);
}

组件类没有办法神奇地识别为字符串,因为它们没有被解析为Angular 2 DI中的字符串(自Angular 1以来已经改变了,其中所有DI单元都注释为字符串).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读