【问题标题】:requirejs: Module name "underscore" has not been loaded yet for context [duplicate]requirejs:尚未为上下文加载模块名称“下划线”[重复]
【发布时间】:2013-12-01 10:42:03
【问题描述】:

当我新加载我的应用程序时,我总是收到此错误,

Error: Module name "underscore" has not been loaded yet for context:
_. Use require([]) http://requirejs.org/docs/errors.html#notloaded  

...,h){c=Error(c+"\nhttp://requirejs.org/docs/errors.html#"+b);c.requireType=b;c.re...

require.js (line 8) TypeError: Backbone.Model is undefined  

var ProjectModel = Backbone.Model.extend({

但是当我点击浏览器上的刷新按钮时它们就消失了。

有人知道为什么吗?我该如何解决?

这是我的config/main/入口js文件,

require.config({
    //By default load any module IDs from js/lib
    baseUrl: 'js',

    paths: {
        jquery: 'lib/jquery/jquery-min',
        underscore: 'lib/underscore/underscore-min',
        backbone: 'lib/backbone/backbone-min',
        text: 'lib/text/text'
    },

    shim: {
        jquery: {
          exports: '$'
        },
        underscore: {
          exports: '_'
        },
        backbone: {
          exports: 'Backbone'
        }
      }
});

require([
    // Load our app module and pass it to our definition function
    'app',
    'jquery',
    'underscore',
    'backbone',

    // Pull in the Collection module.
    'collection/contacts'
], function(App){
    // The "app" dependency is passed in as "App"
    App.initialize();
});

我错过了什么吗?

【问题讨论】:

    标签: javascript jquery backbone.js requirejs underscore.js


    【解决方案1】:

    试试这个:

    shim: {
            jquery: {
              exports: '$'
            },
            underscore: {
              deps:["jquery"],
              exports: '_'
            },
            backbone: {
              deps:["jquery"],
              exports: 'Backbone'
            }
          }
    

    【讨论】:

    • 谢谢!现在效果很好!我可以问一下为什么这种变化会产生如此大的不同 - deps:[...], 是什么意思?谢谢!
    • deps 定义了 AMD 模块的依赖关系,因为您在主干内部没有 jquery 的“实例”,并且您告诉主干依赖于 jquery。很高兴帮助你:) @lauthiamkok
    • 除非加载一个古老版本的 jquery,否则 jquery 不需要 shim 并且填充它可能最终会导致问题。
    • 这似乎是重复的:stackoverflow.com/questions/16774214/… 添加主干依赖项也帮助了我:主干:{ deps: ["underscore", "jquery"], exports: "Backbone" },
    猜你喜欢
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    相关资源
    最近更新 更多