如何正确使用MatTableExporterModule导出数据到excel?

How to correctly use MatTableExporterModule to export data to excel?

我在 Angular 9 中使用 mat-table-exporter npm 模块导出 mat table 组件。

我可以使用 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"
    }
  });
}