Requirejs 可以从一个函数调用另一个函数吗?(分开保存文件)

Requirejs 可以从一个函数调用另一个函数吗?(分开保存文件),requirejs,Requirejs,我知道我可以随时调用任何文件来调用其函数(如果我错了,请纠正我)。我决定通过创建两个模块(one和two)来测试这一点,并从one调用嵌套在two中的函数。这是可行的,但我无法从函数two调用函数one Main.js requirejs.config({ baseUrl: "js" }); require([ "script/one" ], function ($, $Ui, hBs, one) { //works fine one.get("I am the initiat

我知道我可以随时调用任何文件来调用其函数(如果我错了,请纠正我)。我决定通过创建两个模块(onetwo)来测试这一点,并从one调用嵌套在two中的函数。这是可行的,但我无法从函数two调用函数one

Main.js

requirejs.config({
  baseUrl: "js"
});

require([
  "script/one"
], function ($, $Ui, hBs, one) {
  //works fine
  one.get("I am the initiator");
});
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;
})
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;
})
one.js

requirejs.config({
  baseUrl: "js"
});

require([
  "script/one"
], function ($, $Ui, hBs, one) {
  //works fine
  one.get("I am the initiator");
});
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;
})
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;
})
two.js

requirejs.config({
  baseUrl: "js"
});

require([
  "script/one"
], function ($, $Ui, hBs, one) {
  //works fine
  one.get("I am the initiator");
});
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;
})
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;
})

为什么会出现此错误?

问题是您创建了一个

在这种情况下,只有一个注入值具有正确的值,另一个值只是未定义。如果你想一想,很明显这是行不通的,因为在第二个中被注入的人应该如何知道第二个,因为当第一个被创建时,他并没有被创建

幸运的是,有一个解决办法。以后使用
require
功能加载依赖项:

define(["require", 'script/one'], function (require, one) {
  var two = function () {
    return {
      serve1: function (msg) {
        one = require('script/one');
        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;
})

请注意,您仍然需要在依赖项数组中引用
'script/one'

我更新了我的代码,就像您的注释一样,但我收到了一个错误,错误是:“未捕获错误:尚未加载上下文的模块名“script/one:”——“我如何处理此问题?”。。我更新了问题中的代码啊,我忽略了您在模块中立即创建了两个新实例,所以“script/one”无法加载。要解决此问题,必须在
serve1
函数中执行require调用。我会更新我的帖子。