【发布时间】:2021-07-26 18:41:24
【问题描述】:
我遇到了 *ngIf 和 @ViewChild 的问题。对于此类其他问题,我尝试了大多数推荐的解决方案,但对我没有任何帮助。
我的 HTML 文件如下:
<div id="main-container" class="page-layout blank p-24" fusePerfectScrollbar fxLayout="column">
<mat-tab-group #tabGroup (selectedTabChange)="onTabChange($event);" fxLayout="row wrap">
<mat-tab label="Some1" *ngIf="arrayNames.includes('Some1')">
<ng-template matTabContent>
<my-table #table></my-table>
</ng-template>
</mat-tab>
<mat-tab label="Some2" *ngIf="arrayNames.includes('Some2')">
<ng-template matTabContent>
<my-table #table></my-table>
</ng-template>
</mat-tab>
<mat-tab label="Some3" *ngIf="arrayNames.includes('Some3')">
<ng-template matTabContent>
<my-table #table></my-table>
</ng-template>
</mat-tab>
</mat-tab-group>
</div>
在我的组件 ts 文件中,我有以下内容:
matTabs = [1, 2, 3];
@ViewChild('tabGroup', {static: false}) tabGroup: MatTabGroup;
data: Array<SomeEntity> = [];
arrayNames: Array<string> = [];
@ViewChild('table', { static: false }) table: AnotherComponent;
ngOnInit() {
this.someService.getAll()
.subscribe((result) => {
this.data = result;
for (let d of this.data) {
this.arrayNames.push(d.name);
}
});
}
ngAfterViewInit(): void {
this.selectedTabLabel = this.tabGroup?._tabs?.first.textLabel;
this.TabChangeService.changeTab(this.selectedTabLabel);
this.table.displayMyTable();
}
因为 this.table.displayMyTable(); 不显示任何内容,因为 'this.table is undefined'
【问题讨论】:
标签: html angular typescript dom viewchild