ES6模块:为什么先前导出的模块不为“;“孩子”;模块

ES6 modules: why a previously exported module is not known by "child" modules?

本文关键字:模块 孩子 为什么 ES6      更新时间:2023-09-26

我正试图为Meteor/Node应用程序构建一个基于模块的体系结构。

我有一个client/main.js正在导入imports/module1/index.js

imports/module1/index.js在导入组件imports/module1/component/component1.js之后再导入imports/module1/api/api.js

总之,简化的树看起来像这个

.client/main.js
 |_imports/module1/index.js
     |_imports/module1/api/api.js
     |_imports/module1/component/component1.js

api.js文件如下所示:

export default {
  myFunction1 (arg1, arg2) {
    // function stuff
  },
  myFunction2 (arg1, arg2) {
    // function stuff
  },
}

我希望能够在imports/module1/component/component1.js中调用myFunction1(ar1,arg2),但它不起作用。我错过了什么?

当前您没有在component1.js文件中导入api.js,因此component1无法访问api中定义的任何函数。

文件的作用域不会"泄漏",因此在这个问题上,每个文件都是完全独立的。main.js文件中的import不会自动将所有导入的模块提供给其子模块——这是没有意义的。你必须导入一些东西才能使用它。