angular 动态组件

angular动态组件,官方文档有详细介绍,这里仅标记几点以备后记:

1、宿主指令:用于动态组件的显示。指令的核心是注入的ViewContainerRef 接口

<ng-template my-derective></ng-template>

2、ComponentFactoryResolver和ViewContainerRef接口:

ComponentFactoryResolver接口提供组件解析功能:
const component = this.c omponentFactoryResolver. resolveComponentFactory(XXX component);

V iewContainerRef 接口提供组件创建功能,返回组件实例:
this.component = this. V iewContainerRef . createComponent( component); //this.component 为 ComponentRef对象

his.component.instance.xxx = ...; 组件属性赋值


3、对应的模块中加入元数据:

entryComponents:{
XXX1component,
XXX2component,
...
}


猜你喜欢

转载自blog.csdn.net/aust_zyl/article/details/77165177
今日推荐