【问题标题】:js bundles and scope confusionjs 捆绑包和范围混淆
【发布时间】:2019-10-07 22:06:13
【问题描述】:

我试图更好地理解包含节点组件和其他 js 的 webpacked 包中的范围。

假设我的条目导入了八个要捆绑的文件: // 入口点 导入'./components/file1'; 导入'./components/file2'; ... 导入'./components/file8';

假设在 file1.js 我有:

// file1.js

let bubbles = () => {
  console.log('likes cats');
};

// or

function bubbles() {
  console.log('likes cats');
}

那么,如果我在 files8 中有这个(最后导入),为什么会引发未定义的错误?如何调用其他导入中声明的函数?

// file8.js

bubbles(); // fails in any file other than file1.js where it's declared.

【问题讨论】:

    标签: javascript node.js scope


    【解决方案1】:

    您需要在您打算从外部访问的函数/原语上明确使用export

    https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export

    【讨论】:

      猜你喜欢
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 2015-08-13
      • 2020-02-16
      • 1970-01-01
      • 2012-08-30
      • 2016-07-13
      相关资源
      最近更新 更多