【发布时间】:2021-09-28 10:46:59
【问题描述】:
我已经实现了一种将文档下载到 Angular 中的 Excel 电子表格中的方法,如下所示。有人可以告诉我如何为以下void 方法实现承诺。
ExportService类:
export class ExportService {
constructor() { }
fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
fileExtension = '.xlsx';
public exportExcel(jsonData: any[][], fileName: string): void {
const x: XLSX.WorkSheet = XLSX.utils.json_to_sheet(jsonData[0]);
const z: XLSX.WorkSheet = XLSX.utils.json_to_sheet(jsonData[1]);
const b: XLSX.WorkBook = {
Sheets: {
'product': x,
'sales': z
},
SheetNames: ['product', 'sales']
};
const excelBuffer: any = XLSX.write(b, { bookType: 'xlsx', type: 'array' });
this.saveExcelFile(excelBuffer, fileName);
}
private saveExcelFile(buffer: any, fileName: string): void {
const data: Blob = new Blob([buffer], { type: this.fileType });
FileSaver.saveAs(data, fileName + this.fileExtension);
}
}
组件中的导出方法如下。
export() {
this.callProgress = true;
this.progressText = "";
this.salesProductForABC().then(() => {
let products: Array<Array<any>> = [];
products.push(this.productContent, this.salesProductContent);
let month = new Date().getUTCMonth() + 1;
let day = new Date().getUTCDate();
let year = new Date().getUTCFullYear();
let fileName = `${month}${day}${year}`;
this.exportService.exportExcel(products, fileName)
this.callProgress = false;
})
}
``
`
【问题讨论】:
-
要让一个 void 函数返回一个 promise(换句话说,让
void异步函数),让它的返回类型为Promise<void>。你指的是哪些方法?exportExcel内部没有任何异步代码(即使用async/await关键字的代码)。 -
@Hcaertnit Iam 指的是 ExportService 类中的方法。如何在其中实现 then()?
-
@Hcaertnit 我之所以要实现一个promise是,我在上面的export()方法中实现了一个加载器,但是我希望加载器加载到excel表格完成下载。跨度>
标签: typescript promise void