【发布时间】:2017-11-19 07:10:12
【问题描述】:
我正在测试 ES6 模块,并希望让用户使用 onclick 访问一些导入的函数:
test.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Module Test</title>
</head>
<body>
<input type="button" value="click me" onclick="hello();"/>
<script type="module">import {hello} from "./test.js";</script>
</body>
</html>
test.js:
export function hello() {console.log("hello");}
当我点击按钮时,开发者控制台显示:ReferenceError: hello is not defined。如何从模块中导入函数,以便它们可用作 onclick 函数?
我正在使用 Firefox 54.0,并将 dom.moduleScripts.enabled 设置为 true。
【问题讨论】:
-
页面加载完毕后能不能打开控制台手动执行hello?
-
这个功能被标记为实验性的,我现在不会使用它。你最好编译(Babel?)你的代码,以确保它可以在任何浏览器中工作,不是吗?
标签: javascript ecmascript-6 es6-modules