Angular Material MatTable已配置仍报错找不到'vendorid'列
问题概述
预期效果
页面正常展示vendorid对应列,不再抛出列不存在的运行时错误
当前异常
控制台持续抛出报错:ERROR Error: Could not find column with id "vendorid"
技术栈说明
前端采用TypeScript开发,基于Angular框架,搭配Angular Material组件库实现表格功能;后端逻辑不在本次问题排查范围内
已完成的自查操作
- 本地通过localhost接口校验数据可用性,确认接口返回的
vendorid字段数据正常、格式正确 - 从TypeScript的
displayedColumns数组中移除vendorid后,其余列渲染无任何报错 - 逐字核对所有使用
vendorid标识的位置拼写,确认无拼写偏差
问题根因
问题出在HTML中列定义的matColumnDef属性值,和displayedColumns数组中声明的列id不匹配:
当前HTML里的Vendor列定义代码为:
<ng-container matColumnDef="employeeid"> <th mat-header-cell *matHeaderCellDef mat-sort-header> <div class="centerheader">Vendor No.</div></th> <td mat-cell *matCellDef="let element"> {{element.vendorid}} </td> </ng-container>
这里定义的列id是employeeid,但TypeScript中displayedColumns的声明为:
displayedColumns: string[] = ['id', 'name', 'vendorid'];
Angular Material表格的渲染逻辑为:严格按照displayedColumns内的列id,匹配同值的matColumnDef定义,两者不匹配时就会抛出“找不到对应列id”的错误。
此前排查时仅核对了单元格内取数的element.vendorid拼写,没有核对matColumnDef的属性值,因此没有定位到偏差。
修复方案
可任选一种对齐方式完成修复:
- 改动最小的方案:将HTML中的
matColumnDef="employeeid"修改为matColumnDef="vendorid",与displayedColumns的声明完全对齐
修正后的列定义代码:<ng-container matColumnDef="vendorid"> <th mat-header-cell *matHeaderCellDef mat-sort-header> <div class="centerheader">Vendor No.</div></th> <td mat-cell *matCellDef="let element"> {{element.vendorid}} </td> </ng-container> - 若列id确需保留
employeeid,则将TypeScript中的displayedColumns数组修改为['id', 'name', 'employeeid'],与HTML列定义对齐
补充规则说明
Angular Material表格的列匹配为严格字符串匹配:matColumnDef的属性值必须和表头、行渲染绑定的列数组内的字符串完全一致,哪怕存在大小写、单字符拼写偏差,都会触发列不存在的报错。
内容的提问来源于stack exchange,提问作者Marnie Rodriguez
相关产品推荐
相关产品推荐

