我在 Angular 9
中使用 mat-table-exporter
npm 模块来导出 mat 表组件。
我可以使用 test
作为导出文件的名称正确导出它。
以前的工作代码:
<mat-table matTableExporter [dataSource]="dataSource" #exporter="matTableExporter">
<button mat-raised-button (click)="exporter.exportTable('csv', {fileName:'test',
Props: {Author: 'myName'}})">
Export
</button>
我还想在我编写了以下代码的导出文件的名称中添加时间戳:
新组件代码:
exportTable(): void {
this.exporter.exportTable('csv', {
filename: `test-${new Date().toISOString()}`,
Props: {
Author: 'myName'
}
})
}
新的 HTML 代码:
<mat-table matTableExporter [dataSource]="dataSource" #exporter="matTableExporter">
<button mat-raised-button (click)="exportTable()">Export</button>
上面的代码抛出一个错误:
TS2552: Cannot find name 'exporter'.
最佳答案
如果你想让 exporter
在你的组件中可用,你需要使用 ViewChild
,你的 html 代码将保持不变,但组件应该更改为:
@ViewChild(MatTableExporterDirective, { static: true }) exporter: MatTableExporterDirective;
exportIt() {
this.exporter.exportTable(ExportType.CSV, {
fileName: "test",
Props: {
Author: "myName"
}
});
}
关于html - 如何正确使用MatTableExporterModule导出数据到excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65711693/