【问题标题】:What is the difference between these ES6 import statements?这些 ES6 导入语句之间有什么区别?
【发布时间】:2021-05-06 18:31:13
【问题描述】:

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";

上面和下面的 import 语句是否都只导入 A、B、C?这些陈述之间有什么区别吗?是否有偏好使用它们中的任何一个?

【问题讨论】:

  • 你在什么环境下运行这些?
  • 如果您分别从'somecomponent/components' 导入ABC,那么它们将是相同的。但是,您从三个不同的模块(/A/B/C)导入,它们可能与另一个模块相同也可能不同。

标签: javascript ecmascript-6 es6-modules


【解决方案1】:

是的,两个导入都将ABC 导入到当前模块中。但是,第一个从一个模块导入它们,第二个从三个不同的模块导入它们——它们可能具有不同的值。

这两种方式仅当 - 且仅当 - 'somecomponent/components' 处的模块有效

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

(如果这些解析到相同的模块)。

【讨论】:

    【解决方案2】:

    没有任何区别。是的,您只能看到 ABC

    至于偏好……随便你。我自己,打字越少越好。

    【讨论】:

    • 不知道四个模块是怎么实现的,怎么能说没有区别呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    相关资源
    最近更新 更多