简体   繁体   English

@ViewChild为空

[英]@ViewChild is null

In my component I have the following: 在我的组件中,我具有以下内容:

@ViewChild('PaginatedTableComponent') userTable: PaginatedTableComponent;

Inside this component I have the following HTML: 在此组件中,我具有以下HTML:

<pag-paginated-table #userTable
       [(shouldUpdate)]="shouldUpdate"
       [route]="academyMembersRoute"
       [dataStructure]="academyMembersdataStructure"
       [adminActions]="adminActions"
       [include]="academyMembersInclude">
</pag-paginated-table>

However, the userTable variable is null . 但是, userTable变量为null

Can anyone tell me what I might be missing? 谁能告诉我我可能会想念什么? Or why this is happening? 还是为什么会这样?

Update 更新

So this is really odd if i call the function in my ngOnInit it shows that the variable is correctly set. 因此,如果我在ngOnInit调用该函数,则表明变量已正确设置,这真的很奇怪。

However, if I wait and call if from the following : 但是,如果我等待并通过以下方式致电:

addUserClose() {
    this.userTable.callRoute();
    this.shouldUpdate = !this.shouldUpdate;
}

it says that the variable is undefined. 它表示该变量未定义。

Probably this is because you are accessing it before it is initiated. 可能是因为您在启动之前正在访问它。 Try to access it in timeout with zero time out or access it in or after 'ngOnInit' function has been fired. 尝试以零超时在超时中访问它,或者在激发“ ngOnInit”功能之后或之后访问它。 Thanks 谢谢

what you give inside the @ViewChild('your_Variable') to be used in html #your_Variable 您在@ViewChild('your_Variable')中给出的内容将在html #your_Variable

Change you component.ts to 将您的component.ts更改为

@ViewChild('PaginatedTableComponent') userTable: PaginatedTableComponent;

And change you component.html to 并将您的component.html更改为

<pag-paginated-table #PaginatedTableComponent
       [(shouldUpdate)]="shouldUpdate"
       [route]="academyMembersRoute"
       [dataStructure]="academyMembersdataStructure"
       [adminActions]="adminActions"
       [include]="academyMembersInclude">
</pag-paginated-table>

您尝试过:

@ViewChild('userTable') userTable: PaginatedTableComponent; // NOT @ViewChild('PaginatedTableComponent') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM