Requirejs + Backbone Uncatch TypeError:无法读取未定义的属性“each”

Requirejs + Backbone Uncaught TypeError: Cannot read property 'each' of undefined

本文关键字:未定义 属性 each 读取 Backbone Uncatch TypeError Requirejs      更新时间:2023-09-26

错误指向主干.js:219库。 该错误在我的主文件中抛出.js在require(['app'],函数中。

主.js文件:

require.config({
  baseUrl: 'scripts',
  paths: {
    app: '../app',
    jquery: 'jquery',
    underscore: 'underscore',
    backbone: 'backbone',
    router: '../router'
  },
  shim: {
        backbone: {
            deps: ['jquery','underscore'],
            exports: 'Backbone'
        }
    }
});
require(['app'], function (App) {
    App.initialize();
});

我的应用.js文件:

define([
  'jquery',
  'underscore',
  'backbone',
  'router' // Request router.js
], function($, _, Backbone, Router){
  var initialize = function(){
    // Pass in our Router module and call it's initialize function
    Router.initialize();
  };
  return {
    initialize: initialize
  };
});

我的路由器.js文件:

define(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) {

  'use strict';
  // Router
  var AppRouter = Backbone.Router.extend({
    routes:{
      "":"list",
      "wines/:id":"wineDetails"
    },
    list:function () {
      // ...
    },
    wineDetails:function (id) {
      // ...
    }
  });
  var initialize =  function(){
    var app_router = new AppRouter;
    Backbone.history.start();
  };
  return {
    initialize: initialize
  };
});

这个问题似乎与这个问题相似,但没有一个建议对我有用。

路由器.js有两个错误。

  1. 使用"定义"代替"要求";
  2. 返回对象必须包含"初始化"方法;

像这样的代码,

define(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) {
  'use strict';
  // Router
  var AppRouter = Backbone.Router.extend({
    routes:{
      "":"list",
      "wines/:id":"wineDetails"
    },
    list:function () {
      // ...
    },
    wineDetails:function (id) {
      // ...
    }
  });
  var initialize =  function(){
    var app_router = new AppRouter;
    Backbone.history.start();
  }
  return {
    initialize: initialize
  };
});

um

require.config({
  baseUrl: 'scripts',
  paths: {
    app: '../app',
    jquery: 'jquery',  //does this actually point to a jquery source file?
    underscore: 'underscore', //does this actually point to a underscore source file?
    backbone: 'backbone', //does this actually point to a backbone source file?
    router: '../router'
  },
  shim: {
        backbone: {
            deps: ['jquery','underscore'],
            exports: 'Backbone'
        }
    }
});
require(['app'], function (App) {
    App.initialize();
});

我的第一个猜测是,在某些时候,RequireJS需要找出jQuery,Backbone,Underscore等的src/可执行文件的位置。