Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何手动调用这些事件?使用组件工厂分解器注入的组件未被激发?_Angular_Angular6 - Fatal编程技术网

Angular 如何手动调用这些事件?使用组件工厂分解器注入的组件未被激发?

Angular 如何手动调用这些事件?使用组件工厂分解器注入的组件未被激发?,angular,angular6,Angular,Angular6,ngOnInit使用组件工厂解析器注入的组件未被激发 @ViewChild('target'{ 阅读:ViewContainerRef })目标; @输入()步骤; @输入()步骤信息; cmpRef:ComponentRef; private isViewInitialized:boolean=false; 建造师( 专用componentFactoryResolver:componentFactoryResolver, 专用编译器:编译器, 私有cdRef:ChangeDetectorRef

ngOnInit使用组件工厂解析器注入的组件未被激发

@ViewChild('target'{
阅读:ViewContainerRef
})目标;
@输入()步骤;
@输入()步骤信息;
cmpRef:ComponentRef;
private isViewInitialized:boolean=false;
建造师(
专用componentFactoryResolver:componentFactoryResolver,
专用编译器:编译器,
私有cdRef:ChangeDetectorRef
) {}
updateComponent(){
如果(!this.isViewInitialized){
返回;
}
if(this.cmpRef){
this.cmpRef.destroy();
}
让factory=this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef=this.target.createComponent(工厂)
this.cmpRef.instance.data=this.stepinfo;
//(componentRef.instance).data=this.step;
//this.cdRef.detectChanges();
}
读了几遍之后,我才知道在这种情况下,我们需要 手动调用这些事件。我正在为如何做到这一点而挣扎


当您使用
ComponentFactoryResolver
手动创建零部件时,必须手动调用您在其上使用的角度生命周期事件

因为这些方法在组件上是公共的,所以可以像调用普通方法一样调用它们。在本例中,我们希望调用
OnInit
生命周期事件,因此如下所示:

updateComponent(){
如果(!this.isViewInitialized){
返回;
}
if(this.cmpRef){
this.cmpRef.destroy();
}
让factory=this.componentFactoryResolver.resolveComponentFactory(this.step);
this.cmpRef=this.target.createComponent(工厂)
this.cmpRef.instance.data=this.stepinfo;

this.cmpRef.instance.ngOnInit();//在这个问题中,你在两个地方拼写错误了
ngOnInit
。你确定你不仅仅是有代码输入错误吗?还有,里面有
ngOnInit
的组件在哪里?很抱歉,这只是一个输入错误。我已经纠正了,谢谢你的提醒。检查这个堆栈闪电参考-用这个方法,mee也可以正常工作。th问题是,你把整个代码都放进了这个编解码器。其他组件也一样。可能是创建一个stackblitz,这样我们就可以看到哪里出了问题