我正在对我的javascript进行网络打包
alert("Loaded out")
function showLoaded() {
alert("Loadedin")
}
尝试调用从 html 加载的函数
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="./bundle.js"></script>
</head>
<body>
<script>showLoaded()</script>
</body>
</html>
警报显示已加载,但我似乎无法调用函数 showLoaded((。 我在这里错过了一些明显的东西。
您也可以通过以下方式公开函数:
alert("Loaded out");
function showLoaded() {
alert("Loadedin");
}
window.showLoaded = showLoaded;
在这里找到答案, 如何运行由 webpack 转换的函数? .在这里找到答案。
我不得不添加
output {
libraryTarget: "this"
}
到我的 webpack 配置,使函数在全局(窗口(对象上可见。
并使用了导出功能
export function showLoaded() {
alert("Loadedin5")
}