angular - 无法读取 MatTable 中未定义的属性“viewContainer”(角度材料表)
问题描述
我正在尝试在组件(对话框)中显示用户表,但出现此错误:
core.js:4610 错误类型错误:无法在 matTable.ngOnDestroy (table.js:1457) 处的 cleanUpView (core.js:6456) at destroyViewTree (core.js:6532) 处读取未定义的属性“viewContainer”。 js:6282) 在destroyLView
HTML:
<table mat-table [dataSource]="usersList" [multiTemplateDataRows]= 'true' matSort id="ExampleTable" color="light">
<ng-container matColumnDef="email">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Email</th>
<td mat-cell *matCellDef="let row">{{row.email}}</td>
</ng-container>
<ng-container matColumnDef="role">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Role</th>
<td mat-cell *matCellDef="let row"><b>{{row.role}}</b></td>
</ng-container>
<ng-container matColumnDef="edit">
<th mat-header-cell *matHeaderCellDef>Edit</th>
<td mat-cell *matCellDef="let row">
<button mat-icon-button aria-label="Example icon button with a vertical three dot icon">
<mat-icon>edit</mat-icon>
</button>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay;" class="example-detail-row"></tr>
</table>
<mat-paginator [pageSizeOptions]="[25, 50, 100, 150, 200, 300, 500]" showFirstLastButtons></mat-paginator>
.TS
import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { ComponentService } from '../component.service';
@Component({
selector: 'app-users',
templateUrl: './users.component.html',
styleUrls: ['./users.component.scss']
})
export class UsersComponent implements OnInit, AfterViewInit {
@ViewChild(MatSort, { static: true }) sort: MatSort;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
usersList: any;
columnsToDisplay = ['email', 'role', 'edit'];
filteredUsers = [];
scrollMode = 'normal'
constructor(
private componentService: ComponentService
) { }
ngOnInit() {
this.componentService.getUsers().subscribe(users => {
users.forEach((user, i, arr) => {
this.filteredUsers.push(user);
if(i === arr.length -1) {
this.usersList = this.filteredUsers;
this.usersList = new MatTableDataSource(users);
this.usersList.paginator = this.paginator;
}
})
})
}
ngAfterViewInit() {
this.usersList.sort = this.sort;
}
}
“@angular/cdk”:“11.0.0”,“@angular/common”:“11.0.0”,“@angular/compiler”:“11.0.0”,“@angular/core”:“11.0.0 ", "@angular/forms": "11.0.0", "@angular/material": "11.0.0", "@angular/material-moment-adapter": "11.0.0",
解决方案
推荐阅读
- jquery - 如何在ajax select2中设置值
- agora.io - Agora Web SDK v4 SEND_VIDEO_BITRATE_TOO_LOW 和 SEND_AUDIO_BITRATE_TOO_LOW
- cassandra - Janus Graph 后端 cassandra 与 Bigtable
- c - 使用 c 和汇编的指针算术
- nginx - Nginx 站点-可用配置
- makefile - 检查 GNU Make 中覆盖变量的有效值
- vba - VBA - MS Access 2016 - 从表单中保存 CSV 并将表单中的值添加到文件名
- python - 如果解决方案不可行,则放宽 Gekko 约束
- prolog - Prolog 毕达哥拉斯三元组
- docker - k8s 应用程序负载均衡器的目标组中的健康检查失败