RequireJS未捕获错误:匿名定义()模块不匹配

RequireJS Uncaught Error: Mismatched anonymous define() module

本文关键字:定义 模块 不匹配 错误 RequireJS      更新时间:2023-10-27

在一个最小的应用程序中使用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)

我无法从文档或这个已回答的问题中找出问题:匿名定义()模块不匹配

感谢

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

你不应该。如果你在谷歌上搜索"未捕获错误:不匹配的匿名定义()模块",你会注意到最上面的链接是RequireJS的常见问题解答,解释

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

--编辑

如果你使用grunt,你可以使用grunt generate来轻松创建基于你自己的自定义模板的模块,当样板文件过载可能会毁掉你的一天:)

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

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

如果您在HTML中手动编写脚本标记以加载包含几个命名模块的脚本,但随后尝试加载一个匿名模块,该匿名模块的名称与手动编写的脚本标记加载的脚本中的一个命名模块的名称相同。

找到了如何摆脱它:直接使用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())
  })