<script type="module"></script>中,可以采用import的方式引入符合ES模块语法的js文件,ES语法现在的浏览器是支持的。不必使用包管理器命令安装(如 pnpm install xxx)否则还涉及到脚手架及编译(如pnpm dev)
<script type="module"></script>中也支持传统的js文件,传统的js文件将所有属性暴露
<script></script>传统引入模式,是不支持ES模块语法的
简单的实例如下:
old.js
const Name="old"
new.js
const MyObj={
name:'new'
}
export default MyObj
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="old.js"></script>
</head>
<body>
</body>
<script type="module">
import MyObj from "./new.js"
console.log('0',MyObj.name)
console.log('0',Name)
</script>
<script>
console.log('1',MyObj.name)
console.log('1',Name)
</script>
</html>
console结果