美文网首页
Javascript导入导出

Javascript导入导出

作者: 王哈哈就很棒 | 来源:发表于2020-07-05 08:30 被阅读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

相关文章

网友评论

      本文标题:Javascript导入导出

      本文链接:https://www.haomeiwen.com/subject/aubmqktx.html