546 JavaScript的 动态 import 导入

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>动态 import </title>
</head>

<body>
  <button id="btn">点击</button>
  <script src="./js/app.js" type="module"></script>
</body>

</html>

app.js

// import * as m1 from "./hello.js";
//获取元素
const btn = document.getElementById('btn');

btn.onclick = function () {
  // import函数的返回值是Promise对象,成功时的值就是导入模块暴露的对象
  import('./hello.js').then(module => {
    module.hello();
  });
}

hello.js

export function hello() {
  alert('Hello');
}

posted on 2020-09-16 17:22  冲啊!  阅读(742)  评论(0编辑  收藏  举报

导航