首页 > 解决方案 > 网页包。在窗口级别调用 Javascript 函数

问题描述

我正在打包我的 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>

已加载由 alert 显示,但我似乎无法调用函数 showLoaded()。我在这里遗漏了一些明显的东西吗?

标签: javascriptwebpack

解决方案


您也可以通过这种方式公开您的函数:

alert("Loaded out");

function showLoaded() {
    alert("Loadedin");
}

window.showLoaded = showLoaded;

推荐阅读