我正在使用带有 Angular 7 的 pdfmake.js(大约 2mb)和 XLSX(大约 1mb),但它们仅在应用程序的一页上使用。当我加载主页时,在开发人员工具中我可以看到 main.js 文件大小约为 8mb,我实现了延迟加载,将 main.js 文件大小减少到 7mb 我可以通过加载 pdfmake.js 和以某种方式进一步减少 main.js 文件吗仅当我单击某个按钮时才显示 XLSX?
最佳答案
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');;
}
关于npm - 我们可以在角度中延迟加载包吗?我可以仅在单击某个按钮时下载所需的包吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60087516/