我知道我可以随时调用任何文件来调用其功能(如果我错了,请纠正我)。我决定通过创建两个模块(一和二)来测试这一点,并从一个调用嵌套在两个中的函数。这可行,但我无法从函数二调用函数一。
主.js
requirejs.config({
baseUrl: "js"
});
require([
"script/one"
], function ($, $Ui, hBs, one) {
//works fine
one.get("I am the initiator");
});
one.js
define(['script/two'], function (two) {
var one = function () {
return {
get: function (msg) {
//works
console.log("get: " + msg);
//works fine
two.serve1("I am from one of one");
},
post: function (msg) {
// i am calling this method from two.js
console.log("post: " + msg);
two.serve2("i am from two of two");
}
}
}
return new one;
})
二.js
define([ 'require', 'script/one'], function (require,one) {
var two = function () {
one = require('script/one'); // throwing error as "Uncaught Error: Module name "script/one" has not been loaded yet for context: _"
return {
serve1: function (msg) {
console.log("2 in serve1 :" + msg)
// calling doesn't
one.post("initiated from one, called in two");
// throws "Uncaught TypeError: Cannot call method 'post' of undefined"
},
serve2: function (msg) {
console.log("2 in serve2 :" + msg)
}
}
}
return new two;
})
为什么我会收到此错误?