junlin
junlin

Reputation: 2035

ES6 modules export and import

foo.js

const a = 1
const b = 2
const c = 3
let d = 4

export default { a, b }
export { c }
export d

What's the difference between exported a, b, c, d

And how to import them correctly?

Does the Babel compilation will effect it?

Upvotes: 0

Views: 96

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138234

You can only export declarations (except for the default export) that means that:

 export d;

is invalid, it has to be:

 export const d = 1;

And how to import them correctly?

 import main, { c, d } from "sth";
 const {a, b} = main;

You can import c and d in the same way, only the object properties a and b can't be accesed directly as you can't destructure objects inside the import statement, therefore you have to destructure them in a new line which is just ugly. To quote Bergi: "Don't do that", instead go with c or d.

Upvotes: 2

Related Questions