未捕获的TypeError:undefined在使用require.js时不是函数

时间:2014-09-26 21:55:36

标签: javascript jquery requirejs

我是require.js的新手,所以我可能没有正确使用它,但据我所知,我无法找到导致错误的原因。

结构

index.html
Scripts\
  - jquery-2.1.1.min.js
  - require.js
  \app
    - main.js
  \lib
    - work-requests.js
    - controller-base.js
    - util.js

的index.html

<script src="Scripts/require.js" data-main="Scripts/app/main"></script>

main.js

requirejs.config({
    baseUrl: 'Scripts/lib',
    paths: {
        jquery: '../jquery-2.1.1.min',
    },
});
requirejs(['jquery', 'util', 'work-requests'], function ($, util, work_requests) {
    $(function () {
    var foo = new work_requests.get_all(); // <-- error "Uncaught TypeError: undefined is not a function
});

工作requests.js

define(['controller-base'], function (controller_base) {
    return function () {
        var that = controller_base('work-requests')

        return that;
    }
});

控制器base.js

define(['jquery'], function ($) {
    return function (controller_name) {
        var that = {};
        that.controller = controller_name;

        that.get_all = function () {
            return $.ajax({
                type: 'get',
                contentType: 'application/json',
                url: _api_root + '/' + that.controller,
            });
        };
        return that;
    };
});

util.js中

define({
  foo: function(data){},
});

2 个答案:

答案 0 :(得分:2)

您的require.js设置完全正常。你有没有尝试改变

var foo = new work_requests.get_all();

var foo = new work_requests();
var bar = foo.get_all();

答案 1 :(得分:-1)

重复。

您正在寻找mainConfigFile: 'path/to/main.js' requireJS optimization: undefined is not a function

相关问题