Skip to content

Instantly share code, notes, and snippets.

@thawkin3
Last active March 4, 2020 23:22
Show Gist options
  • Save thawkin3/78534ce6be2125a3ad3e0b780bff21cc to your computer and use it in GitHub Desktop.
Save thawkin3/78534ce6be2125a3ad3e0b780bff21cc to your computer and use it in GitHub Desktop.
Using a dynamic import to lazy load some JS
const exportPdfButton = document.querySelector('.exportPdfButton')
exportPdfButton.addEventListener('click', () => {
import('./pdf-download.js')
.then(module => {
// call some exported method in the module
module.exportPdf()
})
.catch(err => {
// handle the error if the module fails to load
})
})
// the 'pdf-download.js' module is only imported once the user click the "Export PDF" button
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment