gpt4 book ai didi

angular - 错误类型错误 : Cannot read property 'createComponent' of undefined in angular 4

转载 作者:太空狗 更新时间:2023-10-29 19:26:41 27 4
gpt4 key购买 nike

我是 Angular 4 的新手,我正在使用“动态组件加载”来制作项目,但是存在用于加载组件的 createComponent 问题。

component上图中有四个部分。

  1. 顶部部分(headerComponent)
  2. 事件部分 (EventsComponent)注意:- 事件部分有两个动态组件(2.1、2.2)
    2.1.特定事件的事件详细信息部分(EventDetailComponent)2.2.用户表单部分(UserFormComponent)

When click on "Event" tab from top header we are hitting event api and showing events in left side bar and then hit event detail api load "2.1 event details" screen. when click on show user then again load "2.2 User Form" dynamic form.

问题:-当点击特定事件的事件详细信息的 api 时,api 现在处于等待状态,然后用户单击“创建事件”选项卡,然后它将创建事件屏幕,然后用户再次单击事件选项卡,然后显示事件详细信息的 createComponent 错误。还单击添加用户按钮,然后再次显示相同的错误。

代码:-

ngAfterViewInit() {
this.getEventList('init');
}

public getEventList(listsource) {
this.appService.getJsonRequest(AppSettings.EVENT_LIST).subscribe(result => {
if (result.status === 200 && result.events) {
this.events = result.events;
this.publicEvents = result.events;
this.events[0].selected = true;
this.selectedEVentInfo = this.events[0];
this.noImgFlag = true;
this.eventDetailFlag = false;
this.eventDetailInfo(listsource);
} else {
this.loaderMsg = AppSettings.EVENT_LIST_ERROR_MSG;
}
// this.loaderFlag = false;
}, err => {
this.loaderMsg = AppSettings.EVENT_LIST_ERROR_MSG;
this.loaderFlag = false;
this.snackBar.open(err, AppSettings.CLOSE, {
duration: AppSettings.DURATION,
});
});
}


eventDetailInfo (event, source: String = null) { // event detail
console.log('### Get event detail information');
if (this.eventDetailFlag === false) {
if ((source === 'list' || source === 'delete' || source === 'stripe') && this.ed !== undefined) { // testing needed
console.log('clear');
this.ed.clear();
}

let edCompFactory: ComponentFactory<any>;
edCompFactory = this.compFactoryResolver.resolveComponentFactory(EdComponent);
const componentRef = this.ed.createComponent(edCompFactory);
(<EdComponent>componentRef.instance).event = this.selectedEVentInfo;
}
}
}

添加用户代码:-

expandAddEventMember(fullWidth) {
if (this.fullWidth === true && fullWidth === true) {
console.log('Before');
if (this.am !== undefined) {
this.am.clear();
}
// setTimeout(() => {
let amCompFactory: ComponentFactory<any>;
amCompFactory = this.compFactoryResolver.resolveComponentFactory(AmComponent);
const componentRef = this.am.createComponent(amCompFactory);
(<AmComponent>componentRef.instance).event = this.selectedEVentInfo;
this.fullWidth = false;
// }, 100);
}
}

错误:- enter image description here

这是我的入口组件

  entryComponents: [
AppComponent,
ForgotModalComponent,
EventDetailsModalComponent,
AddManagerModalComponent,
AddAdminModalComponent,
CopyLinkModalComponent,
OrderSummaryModalComponent,
EventPreviewModalComponent,
PaidMemberModalComponent,
SendMessageModalComponent,
TicketInfoModalComponent,
AddPaidMemberModalComponent,
MessageModalComponent,
TicketCheckInDetailModalComponent,
SaleItemModalComponent,
SaleItemListModalComponent,
ApmComponent,
PmComponent,
AsmComponent,
SlmComponent,
TiComponent,
EdComponent,
AmComponent,
SihComponent,
ImageModalComponent,
GoogleMapModalComponent
]

最佳答案

你只需要确保 amem 在创建组件之前被实例化。我假设它们可以是未定义的,因为在你的代码中,你已经检查如果 amem 在某个阶段被实例化 if(this.am !== undefined)(在调用 clear 之前) >),但不是为了调用 createComponent。只是你需要在 if

中包含我们的 createComponent 代码
  eventDetailInfo(event, source: String = null)
{ // event detail
console.log('### Get event detail information');
if (this.eventDetailFlag === false)
{
if ((source === 'list' || source === 'delete' || source === 'stripe') && this.ed !== undefined)
{ // testing needed
console.log('clear');
this.ed.clear();
/*} <======= Remove this bracket*/

let edCompFactory: ComponentFactory<any>;
edCompFactory = this.compFactoryResolver.resolveComponentFactory(EdComponent);
const componentRef = this.ed.createComponent(edCompFactory);
(<EdComponent>componentRef.instance).event = this.selectedEVentInfo;
}/* <========== Add it here */

}
}

同样适用于addUser

  expandAddEventMember(fullWidth)
{
if (this.fullWidth === true && fullWidth === true)
{
console.log('Before');
if (this.am !== undefined)
{
this.am.clear();
/* } <=== Remove this bracket **/
let amCompFactory: ComponentFactory<any>;
amCompFactory = this.compFactoryResolver.resolveComponentFactory(AmComponent);
const componentRef = this.am.createComponent(amCompFactory);
(<AmComponent>componentRef.instance).event = this.selectedEVentInfo;
this.fullWidth = false;
} /** <==== Add it here */

}
}

编辑:关于您关于规范化的评论,您可能应该提出另一个问题,因为这是一个完全不同的问题。以下一段代码可能不起作用(未经测试),但可以让您了解一种方法

@Injectable()
export class CompFactoryHelper
{
constructor(private resolver: ComponentFactoryResolver)
{

}

public createAndAddComponent(containerRef: ViewContainerRef, eventInfo : any, type: any)
{
containerRef.clear();
let compFactory = this.resolver.resolveComponentFactory(type);
const componentRef = containerRef.createComponent(compFactory);
(componentRef.instance as any).event = eventInfo;
}
}

在你的组件中

 expandAddEventMember(fullWidth)
{
if (this.fullWidth === true && fullWidth === true)
{
console.log('Before');
if (this.am !== undefined)
{
this.compFactoryHelper.createAndAddComponent(this.am, this.selectedEVentInfo, AmComponent);

}

}
}

关于angular - 错误类型错误 : Cannot read property 'createComponent' of undefined in angular 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49197557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com