我正在尝试 http://browserify.org/ 中的示例并尝试按如下方式进行函数调用:
我的 html 是:
<!DOCTYPE html>
<html>
<head>
<title>Test Browserify</title>
<script src="bundle.js"></script>
</head>
<body>
<button onclick="hello()">test</button>
</body>
</html>
我的 javascript 是:
var unique = require('uniq');
var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];
console.log(unique(data));
function hello(){
alert("here");
}
我做了 browserify main.js -o bundle.js,所以我可以成功使用 require。
但是当我点击按钮时,出现错误:
“ Uncaught ReferenceError :你好未定义”
任何建议将不胜感激!
最佳答案
Browserifies 的主要目的是使 JavaScript 模块在私有(private)范围内,因此它无法看到您正在尝试做什么。
尝试使用
global.hello = function() { alert("hello");}
参见 defining global variable for browserify .
一般来说,这是一种不好的做法,您应该改为从模块中导出公共(public)属性,并通过所需的模块引用来引用它们。
关于javascript - 使用 browserify,未捕获的 ReferenceError : function is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30522849/