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

标签 html angular npm angular-material interpolation

我在 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/

相关文章:

html - -webkit-转换 : scale() not displaying properly in OSX Safari

javascript - Angular 响应数据

javascript - 选择 span 元素中的文本并复制到剪贴板

node.js - 替换 NPM/Node 中的包

node.js - NPM 无法安装模块,mkdir 拒绝权限

Typescript 本地依赖项(多个项目)

javascript - 存储 JSON : Javascript vs HTML

javascript - 使用页面部分进行层次结构的动态面包屑

javascript - 调用 forRoot 时 Angular 延迟加载不起作用

html - Internet Explorer 9 拖放 (DnD)