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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20239341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:03:51  来源:igfitidea点击:

RequireJS Uncaught Error: Mismatched anonymous define() module

javascriptbackbone.jsrequirejs

提问by user3036808

Using RequireJS with Backbone in a minimal app, I always get

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

Uncaught Error: Mismatched anonymous define() module

even though the app continues to work. Here it is: https://assets.site44.com/admin/

即使应用程序继续工作。这是:https: //assets.site44.com/admin/

I'm including jQuery, underscore, backbone in index.html, since I want to shorten the define() boilerplate in each view/model.

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

https://assets.site44.com/admin/js/main.jsconsists of

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)

I cannot figure out the issue from docs or this answered question: Mismatched anonymous define() module

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

Thank you

谢谢

回答by Creynders

I'm including jQuery, underscore, backbone in index.html, since I want to shorten the define() boilerplate in each view/model.

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

You shouldn't. If you google "Uncaught Error: Mismatched anonymous define() module"you'll notice the topmost link is to the FAQ of RequireJS explaining that

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

If you manually code a script tag in HTML to load a script with an anonymous define() call, this error can occur.

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

--EDIT

- 编辑

If you're using grunt you can use grunt-generateto easily create modules based on your own custom templates, when boilerplate overload threatens to ruin your day :)

如果您使用 grunt,您可以使用grunt-generate轻松创建基于您自己的自定义模板的模块,当样板超载可能会毁了您的一天时:)

Disclaimer: I wrote the Grunt plugin.

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

回答by user3036808

Found how to get rid of it: use require() directly, rather than inside the ready handler

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

  })