【问题标题】:RequireJS Uncaught Error: Mismatched anonymous define() moduleRequireJS 未捕获错误:不匹配的匿名定义()模块
【发布时间】:2013-11-27 10:04:50
【问题描述】:

在一个最小的应用程序中使用 RequireJS 和 Backbone,我总是得到

Uncaught Error: Mismatched anonymous define() module

即使应用程序继续运行。这里是: https://assets.site44.com/admin/

我在 index.html 中包含 jQuery、下划线、主干,因为我想缩短每个视图/模型中的 define() 样板。

https://assets.site44.com/admin/js/main.js 包括

 var l = console.log.bind(console)

var app
//l("AA")

require.config({
  paths: {
    // Major libraries
    /*jquery: 'libs/jquery/jquery-min',
    underscore: 'libs/underscore/underscore-min', // https://github.com/amdjs
    backbone: 'libs/backbone/backbone-min', // https://github.com/amdjs
*/
    // Require.js plugins
    text: 'text'

  }

})


function initApp() {
  console.log("BB")

  require([
    'views/AppView'
  ], function(AppView){
    l("CC")

    app = new AppView()
    $("#app").html(app.render())

  })
}

$(document).ready(initApp)

我无法从文档或已回答的问题中找出问题: Mismatched anonymous define() module

谢谢

【问题讨论】:

  • 您的代码有很多语法错误,尤其是缺少分号。我真的建议先把这些整理出来。也许取消注释到 jQuery、Underscore 和 Backbone 的路径,因为您的代码依赖于它们被包含在内,并将它们包含在您的 AppViewMyModel define() 调用中。
  • 嗨 Simon,我想避免在每个视图/模型/集合中定义 jQuery、Underscore 和 Backbone。我不确定您指的是什么语法错误——添​​加分号不会使该错误消失。
  • 您不需要在每个视图、模型和集合中都定义它们,但您肯定需要在所有这些中定义 Backbone。如果您信任 JavaScript 固有的不可靠的自动分号插入功能,则可以省略分号。不过,不确定您为什么要这样做。这是难以跟踪的常见错误来源。

标签: javascript backbone.js requirejs


【解决方案1】:

我在 index.html 中包含 jQuery、下划线、主干,因为我想缩短每个视图/模型中的 define() 样板。

你不应该。如果你 google "Uncaught Error: Mismatched anonymous define() module" 你会注意到最上面的链接是 RequireJS 的常见问题解答,解释了这一点

如果您在 HTML 中手动编写脚本标记以通过匿名 define() 调用加载脚本,则可能会发生此错误。

--编辑

如果您正在使用 grunt,您可以使用 grunt-generate 根据您自己的自定义模板轻松创建模块,当样板文件过载可能会毁了您的一天:)

免责声明:我编写了 Grunt 插件。

【讨论】:

  • 感谢 Creynders,但我对此并不满意。我只想拥有更少的代码,而不是(轻松)为我生成代码的工具。
  • @user3036808 您是否尝试过通过window 对象访问它们? IE。 window.$。我不会推荐它。为什么那几行额外的代码让你如此困扰?
  • 1.我希望尽可能少的样板。 2. 该解决方案不适用于处理jQuery插件 比较一下:`define([ 'jquery', 'underscore', 'backbone', 'models/MyModel', 'text!templates/mainTemplate.html' ], function( $, _, Backbone, MyModel, mainTemplate){ ` to this: `define([ 'models/MyModel', 'text!templates/mainTemplate.html' ], function(MyModel, mainTemplate){ `
  • 恐怕没有其他选择了。
【解决方案2】:

如果您在 HTML 中手动编写脚本标记以通过匿名 define() 调用加载脚本,则可能会发生此错误。

如果您在 HTML 中手动编写脚本标记以加载具有几个命​​名模块的脚本,但随后尝试加载一个匿名模块,该模块最终与由手动编码的脚本标签。

【讨论】:

    【解决方案3】:

    找到了摆脱它的方法:直接使用 require(),而不是在准备好的处理程序中

    var l = console.log.bind(console)
    
    var app
    //l("AA")
    
    require.config({
      paths: {
        // Major libraries
        /*jquery: 'libs/jquery/jquery-min',
        underscore: 'libs/underscore/underscore-min', // https://github.com/amdjs
        backbone: 'libs/backbone/backbone-min', // https://github.com/amdjs
    */
        // Require.js plugins
        text: 'text'
    
      }
    
    })
    
    
      require([
        'views/AppView'
      ], function(AppView){
        l("CC")
    
        app = new AppView()
        $("#app").html(app.render())
    
      })
    

    【讨论】:

    • Argh... 错误仍然出现在大约 30% 的情况下,显然是随机的。
    • 发现问题并永久修复:必须在 require.js 之前包含 less.js
    • 在 require.js 之前包含 less.js 不是“正确的解决方案”——让requirejs.config() 处理less.js,必要时可能使用垫片。
    猜你喜欢
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 2014-07-10
    • 2012-04-05
    • 1970-01-01
    • 2016-02-28
    • 2013-05-29
    • 2015-03-24
    相关资源
    最近更新 更多