【发布时间】:2017-10-18 12:09:45
【问题描述】:
我是 ES6 模块的新手,请告诉我如何从网页上的 ES6 模块导入函数,然后在按钮的点击事件中使用该函数。
到目前为止,我已经在一个名为 (ES6Part3Module.js) 的文件中定义了这个模块:
export function sayHello() {
return "hi there folks";
}
然后在我的html页面中我以这种方式加载了模块:
`<script src="ES6Part3Module.js" type="module"`>`</script`>
我现在想通过某种方式为页面上的按钮创建点击处理程序:
`<button id="sayHelloButton"`>Say Hello`</click`>
从我的 ES6 模块中调用 sayHello() 函数。
我尝试在我的页面上创建一个本地(模块)脚本:
`<script type="module"`>
import sayHello from "ES6Part3Module.js";
window.hello = sayHello;
`</script`>
然后在加载时运行的页面上有一些 jquery:
$( function() {
$("#sayHelloButton").click(function() {
alert(hello());
});
});
但是当点击html页面上的按钮时,报错,说没有定义hello。
请你告诉我我做错了什么?
【问题讨论】: