Announcement

Collapse
No announcement yet.

What are dynamic imports

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • What are dynamic imports

    What are dynamic imports

    The dynamic imports using import() function syntax allows us to load modules on demand by using promises or the async/await syntax.

    Code:
    import('./Module').then(Module => Module.method());

  • #2
    use cases for dynamic imports
    1. Import a module on-demand or conditionally. For example, if you want to load a polyfill on legacy browser

    Code:
    if (isLegacyBrowser()) { import(···) .then(···); }
    1. Compute the module specifier at runtime. For example, you can use it for internationalization.

    Code:
    import(`messages_${getLocale()}.js`).then(···);
    1. Import a module from within a regular script instead a module.

    Comment

    Related Topics

    Collapse

    Working...
    X