【问题标题】:how can access require.js module's function?如何访问 require.js 模块的功能?
【发布时间】:2013-06-05 09:30:08
【问题描述】:
我想使用 a.js 模块的 test();
我的方法:
- a.js -
define(function (require, exports, module) {
function test(val) {
alert(val);
}
window.test = test;
}
还有其他方法吗?
对不起,我的英语很差,希望你能理解,谢谢!
【问题讨论】:
标签:
javascript
requirejs
js-amd
【解决方案1】:
您通常不应该从 RequireJS 模块中分配给全局变量(即在 window 上设置属性)。
/path/test.js
define(function() {
function test(val) {
alert(val);
}
return test;
}
/path/app.html
<!-- import requirejs ... -->
<script>
// Use the return value from the "test" module.
require(["test"], function(testFn) {
testFn("hello");
});
</script>