我在最基本的级别上使用 es6 模块功能时遇到了麻烦。
查看 MDN 文档,我看到了使用该功能的许多变体,但它们都不适合我。考虑以下代码片段:
//importer.js
"use strict";
import * as imported from "./exporter";
imported.foo();
//exporter.js
"use strict";
export const foo = () => console.log('success');
//main.html
<html>
<head>
<script src = "./importer.js"></script>
</head>
</html>
我希望以下代码记录“成功”,这样我就知道“导入的模块”成功地从“导出的模块”导入了 foo 函数。
我还尝试了一些其他变体,例如带括号 {} 的导出或默认导出,就像 MDN 中所写的那样。
显然我错过了一些基本的东西。 我们将非常感谢您的帮助。
最佳答案
在您的 html 脚本元素中,请添加 type="module"
属性,在您的导入器文件中,您需要添加文件扩展名
import * as imported from "./exporter.js";
关于javascript - 如何使用es6模块导入导出js文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56476154/