net*_*djw 4 typescript angular-material angular angular-material-table
我正在使用 Angular 9.0.4 和 @angular/material 9.1.3,我想显示一个基本的材料表:
<table mat-table [dataSource]="devizas" class="mat-elevation-z8">
<!-- name -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef>Name</th>
<td mat-cell *matCellDef="let element"> {{ element.name }} </td>
</ng-container>
<!-- code -->
<ng-container matColumnDef="code">
<th mat-header-cell *matHeaderCellDef>Code</th>
<td mat-cell *matCellDef="let element"> {{ element.code }} </td>
</ng-container>
</table>
Run Code Online (Sandbox Code Playgroud)
我在 app.module.ts 中导入了这个:
import { MatTableModule } from '@angular/material/table';
@NgModule({
imports: [
MatTableModule,
],
})
Run Code Online (Sandbox Code Playgroud)
在我的 component.ts 中:
export class DevizaListComponent implements OnInit {
devizas: DevizaInterface[] = [
{
name: 'dollar',
code: 'USD' ,
}
];
constructor() { }
ngOnInit() { }
}
Run Code Online (Sandbox Code Playgroud)
现在我在浏览器中收到此错误消息:
错误:缺少页眉、页脚和行的定义;无法确定应呈现哪些列。在 getTableMissingRowDefsError (table.js:996) at MatTable.push../node_modules/@angular/cdk/ ivy_ngcc /fesm5/table.js.CdkTable.ngAfterContentChecked (table.js:1296)
官方文档没有说明任何相关内容。任何的想法?
pro*_*lic 12
您在代码中缺少以下行html:
<tr mat-header-row *matHeaderRowDef="columndefs"></tr>
<tr mat-row *matRowDef="let row; columns: columndefs;"></tr>
Run Code Online (Sandbox Code Playgroud)
在 component.ts 文件中创建一个具有列定义的数组,如下所示:
columndefs : any[] = ['name','code'];
更新表代码如下:
<tr mat-header-row *matHeaderRowDef="columndefs"></tr>
<tr mat-row *matRowDef="let row; columns: columndefs;"></tr>
Run Code Online (Sandbox Code Playgroud)
这是演示:演示
尝试这个!
<table mat-table [dataSource]="devizas" class="mat-elevation-z8">
....
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12015 次 |
| 最近记录: |