我们可以延迟加载 Angular 中的包吗,我可以只在单击某个按钮时才下载所需的包吗?



我正在使用pdfmake.js(大约2mb(和XLSX(大约1mb(和角度7,但它们只在应用程序的一个页面上使用。 当我加载主页时,在开发人员工具中我可以看到 main.js 文件大小约为 8MB,我实现了延迟加载,将 main.js 文件大小减小到 7mb 我可以通过加载 pdfmake 和 XLSX 来以某种方式进一步减少主文件.js文件.js只有当我点击一些按钮?

pdfMake:any;

async loadPdfMaker() {
if (!this.pdfMake) {
const pdfMakeModule = await import('pdfmake/build/pdfmake');
const pdfFontsModule = await import('pdfmake/build/vfs_fonts');
this.pdfMake = pdfMakeModule.default;
this.pdfMake.vfs = pdfFontsModule.default.pdfMake.vfs;
}
}

async generatePdf() {
await this.loadPdfMaker();
const def = { content: 'A sam`enter code here`ple PDF document generated using Angular and PDFMake' };
this.pdfMake.createPdf(def).download('optionalName.pdf');;
}

您可以通过导入库的缩小版本来进一步减小大小。

import * as pdfMake from 'pdfmake/build/pdfmake.min';

最新更新