Tom :
I'm trying to export a frozen object within a dynamically imported module, and it's failing.
My main module is:
import ('./hello.mjs').then(function (o) {
o.hello();
});
My imported module, hello.mjs, has this code:
function hello () {
console.log('Hello');
}
export default Object.freeze({ hello });
The program fails with this error in the console:
TypeError: o.hello is not a function at main.mjs:2
However, if I use a simple export, like this:
function hello () {
console.log('Hello');
}
export { hello };
...then it works. Exporting default frozen objects works with non-dynamic module imports. Why do dynamic module imports behave differently, and what can I do to correct this?
T.J. Crowder :
The fulfillment value of the promise from dynamic import is the module namespace object for the module. (See FinishDynamicImport in the spec.) To access the default export, use the property default
on it, perhaps via destructuring:
import ('./hello.mjs').then(function ({default: o}) {
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^
o.hello();
});
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=302505&siteId=1