index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- 这里需要指定type为module才能使用import导入 -->
<script src="index.js" type="module"></script>
</body>
</html>
utils.js
export default (a, b) => {
return a + b;
}
export let name = "王哈哈";
let host = '127.0.0.1';
let port = 3306;
export {
host,
port
}
index.js
// 使用默认导入
import add from "./utils.js"
console.log(add(3, 5));
// 导入指定名称的变量
import { host, port, name } from "./utils.js"
console.log(`host=${host} port=${port} name=${name}`);
// 导入所有的变量为一个对象,使用对象调用
import * as utils from "./utils.js"
console.log(utils.default(1, 3))
console.log(utils.host, utils.port, utils.name);
浏览器console
![](https://img.haomeiwen.com/i7985835/12d0ee1a4e3e81c2.png)
网友评论