首页 > 解决方案 > 添加导入后,我的 chrome 扩展程序立即停止工作

问题描述

当前代码有效,并会在单击按钮时发出警报,但如果我移动myAlert到不同的文件并导入它,它将停止工作。有人知道发生了什么吗?

清单.json

{
  "name": "Getting Started Example",
  "version": "1.0",
  "description": "Build an Extension!",
  "browser_action": {
    "default_popup": "popup.html"
  },
  "manifest_version": 2
}

popup.html

<!DOCTYPE html>
<html>
  <head>
    <style>
      button {
        height: 30px;
        width: 30px;
        outline: none;
      }
    </style>
  </head>
  <body>
    <button id="changeColor"></button>
    <script type="module" src="popup.js"></script>
  </body>
</html>

popup.js

// import { myAlert } from "./alert";
function myAlert() {
    alert("hello");
}

let changeColor = document.getElementById('changeColor');
changeColor.onclick = myAlert;

标签: javascriptgoogle-chrome-extension

解决方案


推荐阅读