javascript - npm webpack-mix - 从文件中调用 javascript 函数
问题描述
我正在尝试访问table.js
文件中定义的函数index.html
。
表.js
let table;
function set_table(table) {
this.table = table;
console.log("function called successfully");
}
export {set_table};
索引.html
<script src="table.js"></script>
<script type="text/javascript">
set_table("test");
</script>
我正在使用 npm webpack mix 来编译table.js
并接收错误Uncaught ReferenceError: set_table is not defined
。
请建议访问table.js
功能的最佳方式index.html
解决方案
模块导出需要设置为window
. 因此,只需导入set_table
脚本 type="module" 然后将其设置window
为window.set_table
<script type="module">
import { set_table } from "./table.js";
window.set_table = set_table;
</script>
推荐阅读
- python - Discord.py 票务系统
- c# - C# WebClient 下载文件到绝对路径
- eclipse - 如何解决在 Eclipse 中调用方法的问题?
- javascript - 找不到模块:无法使用 npm 包解析“反应”
- javascript - 在过滤两个日期之间的数据时从客户端或服务器端发送日期?
- ios - Flutter IOS 应用程序在部署到测试飞行时未收到 FCM 通知
- app-store-connect - 为在美国境外使用应用程序所需的 HTTPS/API 调用提供出口合规性文档文件?
- python - 我如何解决python中的cplex导入问题
- apache - 如何通过 URL 禁用直接端口访问
- javascript - 你怎么能 module.export 一个对象?