javascript - js 捆绑包和范围混淆
问题描述
我试图更好地理解带有节点组件和其他 js 的 webpacked 包中的范围。
假设我的入口导入了八个要捆绑的文件: // entry point import './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.org/en-US/docs/web/javascript/reference/statements/export
推荐阅读
- javascript - 我如何为 iframe 创建一个音量 div,我可以在不单击 iframe 音量图标的情况下控制 iframe 音量?
- c++ - 构建 Rcpp 包时未在 Makevars.win 中读取 LDFLAGS
- marie - 我将如何修复此代码以乘以负数?
- mysql - 无法创建带有卷的 MYSQL docker 容器
- c# - 如何尽可能快地同时填写多个列表?
- python - 使用 WPF 执行 python 脚本
- java - 无法使用 API for Java 在 KML 文件中创建地标和样式
- firebase - Firefox 拒绝加载 firebase 身份验证脚本
- kotlin - Jetpack Compose Desktop:可滚动列/LazyColumn
- r - 散点图上的颜色线 - R