I know 2 writing conventions of importing modules in ES2022 module.
import mod from 'module'
or
import * as mod from 'module'
It looks like the same movement. What's the difference? And which one should I use?
Thanks
CodePudding user response:
The former imports the default export. The latter imports all the named exports as well.
Which you use depends on what you want to do. There isn't a "right" approach.
module.js
export const a = 'a';
export const b = 'b';
const c = 'c';
export default c;
index.js
import x from './module.js';
import * as y from './module.js';
console.log({ x, y });
output
{
x: 'c',
y: [Module: null prototype] { a: 'a', b: 'b', default: 'c' }
}