嗯,是的,我很抱歉这个问题,它重复了很多次,我找到了很多解决方案,但他们没有帮助。也许,我从一开始就做错了什么。 我在index.html中加载了require.js。它工作正常。
<script data-main="js/main" src="js/require.js"></script>
在main.js中我加载了jQuery和我的module.js
require(['jquery', 'module'], function ($, module) {
$(document).ready(function () {
alert('yohoho');
module.foo();
});
});
在module.js中我有
define('module', [], function () {
var module = {
foo: function () {
alert('foo');
},
bar: function () {
alert('bar');
}
};
return module;
});
因此,jQuery加载正常并正常工作(警告'yohoho')。模块不起作用。作为参数传递的变量模块已定义,并具有id,url等字段,但没有foo或bar。错误是“module.foo不是函数”
附:诸如“将名称更改为./module”或“无功能返回对象”或“在文档外使用foo”等解决方案无济于事。
UPD此代码确实有效
main.js
require(['one', 'two', 'three'], function (one, two, three) {
one.foo();
two.foo();
three.foo();
});
one.js
define('one', [], function () {
return {
foo: function () {
alert('one foo');
}
}
})
two.js和three.js具有相同的代码,具有不同的模块名称。
答案 0 :(得分:0)
我将文件module.js重命名为xmodule.js,并将所有引用重命名为&#39; xmodule&#39;它现在工作正常。