Javascript导入导出

2020-07-05  本文已影响0人  王哈哈就很棒

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

console.png
上一篇 下一篇

猜你喜欢

热点阅读