我正在尝试动态创建一个组件,并像这样设置Angular6代码
HTML:
<div *ngIf="defaultToTrue">
<div #entry></div>
</div>
技术支持:
在构造函数之前
@ViewChild ('entry', {read: ViewContainerRef}) entry: ViewContainerRef;
组件创建:
const entryFormFactory = this.resolver.resolveComponentFactory(entryFormComponent);
const entryComponent = this.entry.createComponent(entryFormFactory);`
如果我把上面的两行放进去
ngAfterContentInit()
,我得到错误:
错误:类型错误:无法读取未定义的属性“CreateComponent”
如果我把它放进去
ngAfterViewInit()
,我得到错误:
表达式在被检查后被更改
只有当我把它包在
setTimeout
我不该这么做。
我错过了什么?