The embarrassment of import es6 & # 8594; export

Is this valid javascript? This is not a mistake and seems to work.

export {default as Chooser} from "./chooser"; 

My interpretation:

  • import default of "./chooser"
  • export result from # 1 as Chooser

Is that what is happening?

+7
javascript ecmascript-6 es6-modules
source share
2 answers

Is this valid JavaScript?

Yes.

Is that what is happening?

Yes.

+3
source share

Your interpretation is correct.

import default from "./chooser"

It is right. The Chooser thing exported by default and when importing you should use the name assigned to it with as ... :

 import { Chooser } from "./chooser"; 

export result from # 1 as Chooser

This is also correct. Name Chooser sets the default new name and exports it.


Let me break this:

 export { default as Chooser } from "./chooser"; 

This means that the file from which it is exported, and default as Chooser exports the default value named Chooser . Now, when importing:

 import { Chooser } from "./chooser"; 

You must specify Chooser to import, since you essentially named the default value.

+1
source share

All Articles