我要用webpack打包原生js,
然后写了个小demo测试一下:
我写了个test.js文件
module.exports = {
alertMsg:function(){
alert(1);
}
}
然后在入口文件entry.js里面导入这个文件
'use strict';
let a = require('./home/test.js');
module.exports = {a};
下面index.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script src="./common/jquery.min.js"></script>
<script src="./bundle.js"></script>
</head>
<body>
<div id="head"></div>
<div>
<button id="btn" onclick="alertMsg()">click</button>
</div>
</body>
<script>
window.alertMsg = a.alertMsg;
</script>
</html>
提示a未定义,我不知道是哪里理解出错,是对es6的module?!
迫切希望得到帮助。。。感谢
entry.js
中加一行代码就行了window.a = a;
a
是一个模块,不是一个全局变量,需要挂在windows
上才可以。