requirejs出错

时间:2015-08-25 13:11:30

标签: javascript requirejs amd

我正在尝试使用requirejs的简单模块。 /js/app.js

define(function (require, exports) {           
  var calculator = require('/js/calculator');                                     
  var result = calulator.add(1, 2);

  console.log(result);                                                               
});

/js/caluator.js

define(function (require, exports) {           
  exports = {
    add: function (one, two) {
      return one+two;
    }
  };
});

当我运行应用时,我收到错误Error: Script error for: calculator
代码来源https://github.com/souparno/modular-js

2 个答案:

答案 0 :(得分:0)

使用return语句确保您的函数在模块底部定义并正确公开:

define(function (require, exports) {

 var calculator = {
     add: function (one, two) {
      return one+two;
     }
   };


  return calculator;
});

答案 1 :(得分:-1)

/js/app.js

define(function (require) {           
  var calculator = require('calculator');                                     
  var result = calulator.add(1, 2);

  console.log(result);                                                               
});

/js/calculator.js

define(function (require) {           
  return {
    add: function (one, two) {
      return one+two;
    }
  };
});