博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular 2] Generate Angular 2 Components Programmatically with entryComponents & ViewContainerRef
阅读量:6805 次
发布时间:2019-06-26

本文共 3816 字,大约阅读时间需要 12 分钟。

You can generate Angular 2 components using a combination of ViewContainer and ComponentFactory, but you must always remember to add the components you want to generate to your list of entryComponents otherwise the compiler will optimize the component class out of your project.

 

ViewChild:

For example, in HomComponent, we created a div with ref "#container", if you log out the element we can see:

import {Component, ViewChild} from '@angular/core';import {SimpleService} from "../../serivces/simple.service";@Component({    moduleId: module.id,    selector: 'home',    template: '
'})export class HomeComponent { @ViewChild('container') container; constructor(private simpleService: SimpleService) { } ngAfterContentInit(){ }}

It is a native Element. 

 

Actually you can View the child in other different way:

@ViewChild('container', {        read: ViewContainerRef    }) container;

"ViewContainerRef": Represents a container where one or more Views can be attached.

 

For example we now want to create component Programmatically:

import {Component, ViewChild, ViewContainerRef, ComponentFactoryResolver} from '@angular/core';import {WidgetThree} from "../widgets/widget-three.component";@Component({    moduleId: module.id,    selector: 'home',    template: '
'})export class HomeComponent { @ViewChild('container', { read: ViewContainerRef }) container; constructor(private resolver: ComponentFactoryResolver) { } ngAfterContentInit(){ const WidgetFactory = this.resolver.resolveComponentFactory(WidgetThree); this.container.createComponent( WidgetFactory ) }}

See 

But now, it won't work:

It says it cannot find WidgetThree, even we already define in Module:

import { NgModule} from '@angular/core';import { CommonModule } from '@angular/common';import {WidgetOneComponent} from './widget-one.component';import {WidgetTwoComponent} from './widget-two.component';import {WidgetThree} from './widget-three.component';@NgModule({    imports: [CommonModule],    declarations: [WidgetOneComponent, WidgetTwoComponent, WidgetThree],    exports: [WidgetOneComponent, WidgetTwoComponent, WidgetThree, CommonModule]})export class WidgetsModule {}

 

The problem for that is because Angualr2 optimize the imports component, check whether it is used in template, if not, then remove the component from the bundle file. Because in HomeComponent tempalte, we didn't use WidgetThree compoennt, so it was removed.

 

To solve the problem, we can use "entryComponents":

import { NgModule} from '@angular/core';import { CommonModule } from '@angular/common';import {WidgetOneComponent} from './widget-one.component';import {WidgetTwoComponent} from './widget-two.component';import {WidgetThree} from './widget-three.component';@NgModule({    imports: [CommonModule],    declarations: [WidgetOneComponent, WidgetTwoComponent, WidgetThree],    entryComponents: [WidgetThree],    exports: [WidgetOneComponent, WidgetTwoComponent, WidgetThree, CommonModule]})export class WidgetsModule {}

 

"entryComponents" just tell angular, no matter what, keep the componet into the bundle, don't remove it.

And now, we can actually create multi WidgetThree component programmatically:

export class HomeComponent {    @ViewChild('container', {        read: ViewContainerRef    }) container;    constructor(private resolver: ComponentFactoryResolver, private simpleService: SimpleService) {    }    ngAfterContentInit(){        const WidgetFactory = this.resolver.resolveComponentFactory(WidgetThree);        this.container.createComponent(WidgetFactory);        this.container.createComponent(WidgetFactory);        this.container.createComponent(WidgetFactory);        this.container.createComponent(WidgetFactory);        this.container.createComponent(WidgetFactory);    }}

 

转载地址:http://lejwl.baihongyu.com/

你可能感兴趣的文章
[BZOJ 2002][Hnoi 2010]Bounce 弹飞绵羊
查看>>
1045 access denied for user 'root'@'localhost' using password yes
查看>>
接口测试基础
查看>>
asp.net+ajax+WebServer 输入自动提示历史记录
查看>>
JDK1.8源码分析之HashMap(一) (转)
查看>>
常见的反爬虫和应对方法 (转)
查看>>
intellij idea 高级用法之:集成JIRA、UML类图插件、集成SSH、集成FTP、Database管理(转)...
查看>>
将Sublime Text 2搭建成一个好用的IDE(转)
查看>>
Java 理论与实践: 正确使用 Volatile 变量(转)
查看>>
[转]解决get方法传递URL参数中文乱码问题
查看>>
维生素和止痛药的区别
查看>>
[HTML5] Canvas绘制简单图片
查看>>
[javaSE] IO流(装饰设计模式)
查看>>
jquery hover 不停闪动 解决(亦为stop()的使用)
查看>>
C#反射取数组单个元素的类型
查看>>
Unity编辑器下获取动画的根运动状态并修改
查看>>
JS如何获取url查询字符串的键和值?
查看>>
20165206 2017-2018-2《Java程序设计》课程总结
查看>>
【SignalR学习系列】5. SignalR WPF程序
查看>>
【2011.09.01】如何使用javaScript代码获取系统时间和日期?
查看>>