【问题标题】:Uncaught Error: Mismatched anonymous define() module: function definition(name, global) [duplicate]未捕获的错误:不匹配的匿名定义()模块:函数定义(名称,全局)[重复]
【发布时间】:2014-07-10 20:28:25
【问题描述】:

我在加载主干的 requirejs 文件时遇到此错误。我尝试加载 r.js,requirejs 优化器,但我仍然坚持使用它。

Uncaught Error: Mismatched anonymous define() module: function definition(name, global){

"use strict";

var PubSub = {
        name: 'PubSubJS',
        version: '1.3.1-dev'

以下是我的js:

define([
'jquery',
'underscore',
'backbone'
],function(){
subAccountRouter = Backbone.Router.extend({
  routes: {
  // Defining the routes
    'sub-accounts': 'subAccountList',
    '*actions': 'defaultAction'
  },
});

似乎对 requirejs 的 define() 调用函数进行了一些更改,不知何故无法弄清楚。有人有想法吗??

编辑:::

下面是 router.js 文件。

    define([
       'jquery',
       'underscore',
       'backbone'
      ],function($, _, Backbone){
          SubAccountRouter = Backbone.Router.extend({
              routes: {
               'sub-accounts': 'subAccountList',
               '*actions': 'defaultAction'
              },


           initialize: function () {
              this.appContainer = $("#subaccount");
    //collections and models
              this.subAccountCollection = null;
            this.subAccountModel = null;
          },

      subAccountList: function(){
        var self = this;
        },
     defaultAction: function(){
        this.subAccountList();
      },
      });

    return {
       initialize: function() {
           Backbone.history.start();

          }
        };
     }); //main func

我在这里做错了什么? 我检查了我的路径,它们似乎是正确的,我仍然不明白为什么这个问题仍然困扰着我..:( 我尝试更改路由的路径,并将参数传递给function($, _, Backbone)(如下面的 sol'n 1 所示)。但是我似乎仍然看到错误。有没有人有其他想法???

【问题讨论】:

    标签: javascript backbone.js requirejs


    【解决方案1】:

    更新

    检查文档后 - 这实际上是the first error they discuss

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

    因此,请确保 index.html 中唯一的 <script> 标记(至少对于任何调用 define() 的脚本)是 requirejs 的标记。

    结束更新

    您需要像这样将参数传递给您的function()

    define([
    'jquery',
    'underscore',
    'backbone'
    ],function(jquery, underscore, backbone){
    subAccountRouter = Backbone.Router.extend({
      routes: {
      // Defining the routes
        'sub-accounts': 'subAccountList',
        '*actions': 'defaultAction'
      },
    });
    

    我最近写了一个super-simple post on setting up requirejs,如果你仍然卡住的话。

    【讨论】:

    • 谢谢,您的文章对解决其他问题很有帮助,我有。但我还是有点卡在这个问题上。
    • 很高兴这篇文章对您有所帮助。我已经更新了我的答案。
    • 谢谢,它应该工作得很好,但又对我不起作用..:(
    • 有什么办法可以摆脱这个问题??我的意思是我仍然可以加载我的 html 页面和其他内容??
    • This article 可能会有所帮助。
    【解决方案2】:

    根据文档,require.js 在以下情况下会爆炸:

    • 你有一个匿名定义(“模块调用define() string ID") 在它自己的脚本标签中(我假设它们实际上是指 全球范围内的任何地方)。
    • 您的模块名称冲突。
    • 您使用加载器插件或匿名模块,但不使用 require.js 的优化器将它们捆绑在一起。

    我在将使用 browserify 构建的捆绑包与 require.js 模块一起包含时遇到了这个问题。解决方案是:

    A.在加载require.js 之前,在脚本标签中加载non-require.js 独立包,或者

    B.使用require.js(而不是脚本标签)加载它们。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 2013-07-07
    相关资源
    最近更新 更多