我正在尝试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");
}我确实对main.js -o bundle.js进行了浏览器化,所以我可以成功地使用require。
但当我单击该按钮时,会出现以下错误:
“未捕获ReferenceError:未定义hello”
任何建议都将不胜感激!
发布于 2015-05-29 14:51:37
Browserifies的主要目的是使JavaScript模块具有私有作用域,因此它无法看到您正在尝试做的事情。
尝试使用
global.hello = function() { alert("hello");}参见defining global variable for browserify。
一般来说,这是一种糟糕的做法,您应该将公共属性导出到模块之外,并通过所需的模块引用来引用它们。
发布于 2020-01-05 01:31:13
只需尝试在js文件中使用global.hello = function() { alert("hello");},然后使用browserify构建它。
https://stackoverflow.com/questions/30522849
复制相似问题