ES6 Dynamic importing with namespace?

Viewed 2173

When using dynamic imports, can I define what I want to import like regular imports?

For example:

import Person from '/classes.js'

As dynamic:

await import('Person from /classes.js') //Incorrect obviously
2 Answers

Dynamic imports will hand you everything from within the module. You can use destructuring the extract the pieces you want.

const { Person } = await import('/classes.js');

You can try this when you need to import some specific file.

const moduleSpecifier = '/classes.js';
import(moduleSpecifier)
.then(someModule => someModule.myFucntion());
Related