简体   繁体   中英

What is the difference between these ES6 import statements?

import { A, B, C } from 'somecomponent/components'

import { A } from "somecomponent/components/A";
import { B } from "somecomponent/components/B";
import { C } from "somecomponent/components/C";

Does both the import statements above and below import only A, B, C? Is there any difference between these statements? Is there a preference to use either of them?

Yes, both imports do import A , B and C into the current module. However, the first imports them from one module, the second imports them from three different modules - which might have different values.

The two ways are only equivalent if - and only if - the module at 'somecomponent/components' does

export { A } from "somecomponent/components/A";
export { B } from "somecomponent/components/B";
export { C } from "somecomponent/components/C";

(and iff these resolve to the same modules).

There is no difference whatsoever. And yes, only A , B , and C are visible to you.

As for preference... whatever you like. Myself, less typing is better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM