[js捆绑和范围混淆
我正在尝试更好地了解包含节点组件和其他js的webpack捆绑包中的范围。
假设我的条目导入八个要捆绑的文件:// 入口点导入'./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.
回答如下:您需要在要从外部访问的函数/基元上显式使用export
:
https://developer.mozilla/en-US/docs/web/javascript/reference/statements/export
[js捆绑和范围混淆
我正在尝试更好地了解包含节点组件和其他js的webpack捆绑包中的范围。
假设我的条目导入八个要捆绑的文件:// 入口点导入'./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.
回答如下:您需要在要从外部访问的函数/基元上显式使用export
:
https://developer.mozilla/en-US/docs/web/javascript/reference/statements/export