定义一个module模块
//module.js
var m="mmm";
var n="nnn";
export {m,n}
HTML文件script标签的type使用“module”
<script type="module" src="module.js"></script>
使用本地服务器
本地文件模式会报错
Access to script at 'file:///C:/temp/module.js' from origin 'null' has been blocked by CORS policy: The response is invalid.
引入模块中的变量并使用
import {m,n} from "./module.js"
var app=document.getElementById("app");
app.innerHTML =m;
完整HTML文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="module" src="module.js"></script>
</head>
<body>
<div id="app"></div>
<script type="module">
import {m,n} from "./module.js"
var app=document.getElementById("app");
app.innerHTML =m;
</script>
</body>
</html>