【问题标题】:Uncaught Error: Mismatched anonymous define() module未捕获的错误:不匹配的匿名定义()模块
【发布时间】:2015-05-12 08:03:22
【问题描述】:

我是一个受虐狂,所以我决定改变我在项目中加载 require.js 的方式。我所做的只是从这个出发(使用 Modernizr.load)......

<script type="text/javascript">

    function requireJsConfig () {
        requirejs.config({
            'baseUrl': '{{ STATIC_URL }}js',
            'paths': {
                'jquery': 'libraries/jquery',
                'backbone': 'libraries/backbone',
                'underscore': 'libraries/underscore',
                'jquery.cookie': 'libraries/jquery-cookie',
                'tinymce': 'tinymce/tinymce.min',
                'react' : 'libraries/react',
                'history': 'history'
            },
            waitSeconds: 30,
            shim: {
                'jquery' : 'jquery',
                'underscore' : {
                    exports: '_'
                },
                'backbone': {
                    deps: ['underscore', 'jquery'],
                    exports: 'Backbone'
                },
                'jquery.cookie': {
                    deps: ['jquery'],
                    exports: '$.cookie'
                },
                'tinymce': {
                    deps: ['jquery'],
                    exports: 'tinymce'
                }
            }
        });

        define('static_url', [], "{{ STATIC_URL }}");
        window.static_url = "{{ STATIC_URL }}";
        define('PUBNUB_SUBSCRIBE_KEY', [], "{{ PUBNUB_SUBSCRIBE_KEY }}");

        require(["teacher"]);
    };

    Modernizr.load({
        load: '{{ STATIC_URL }}js/require.js',
        complete: function() {
            requireJsConfig();
        }
    });

</script>

...到这个(不使用 Modernizr.load)...

<script type="text/javascript" src="{{ STATIC_URL }}js/require.js"></script>

<script type="text/javascript">

    function requireJsConfig () {
        requirejs.config({
            'baseUrl': '{{ STATIC_URL }}js',
            'paths': {
                'jquery': 'libraries/jquery',
                'backbone': 'libraries/backbone',
                'underscore': 'libraries/underscore',
                'jquery.cookie': 'libraries/jquery-cookie',
                'tinymce': 'tinymce/tinymce.min',
                'react' : 'libraries/react',
                'history': 'history'
            },
            waitSeconds: 30,
            shim: {
                'jquery' : 'jquery',
                'underscore' : {
                    exports: '_'
                },
                'backbone': {
                    deps: ['underscore', 'jquery'],
                    exports: 'Backbone'
                },
                'jquery.cookie': {
                    deps: ['jquery'],
                    exports: '$.cookie'
                },
                'tinymce': {
                    deps: ['jquery'],
                    exports: 'tinymce'
                }
            }
        });

        define('static_url', [], "{{ STATIC_URL }}");
        window.static_url = "{{ STATIC_URL }}";
        define('PUBNUB_SUBSCRIBE_KEY', [], "{{ PUBNUB_SUBSCRIBE_KEY }}");

        require(["teacher"]);
    };

    requireJsConfig();

</script>

...现在我明白了:

未捕获的错误:不匹配的匿名 define() 模块:函数 (){return r}

FWIW,这是发生错误的地方(在 require.js 中):

function intakeDefines() {
    var args;

    //Any defined modules in the global queue, intake them now.
    takeGlobalQueue();

    //Make sure any remaining defQueue items get properly processed.
    while (defQueue.length) {
        args = defQueue.shift();
        if (args[0] === null) {
            return onError(makeError('mismatch', 'Mismatched anonymous define() module: ' + args[args.length - 1]));
        } else {
          //args are id, deps, factory. Should be normalized by the
          //define() function.
          callGetModule(args);
        }
    }
}

顺便说一句,我已经验证了these 都不适用。

为什么这个看似无害的变化会导致我的项目中的核弹爆炸? (是的,一枚真正的核弹引爆了……仿佛数以百万计的声音突然惊恐地呼喊,又突然沉默了……)

【问题讨论】:

    标签: javascript requirejs modernizr


    【解决方案1】:

    让我们看看我能不能比帕特里克解释得更好......

    对于underscore,您不需要shim,因为它会检测它是否使用AMD 加载程序运行,如果是,则调用define不要将shim 用于调用define 的模块。

    您还应该删除 jQuery 的 shim。除非您使用的是旧版本,否则它还会检测到它正在使用 AMD 加载程序运行并调用 define。此外,shimjquery: "jquery" 毫无意义。 (我能想到的最接近有意义的shimjquery: ["jquery"],这意味着“jquery 取决于jquery”。)

    至于为什么它以前会起作用...当您将shim 与调用define 的模块一起使用时,您进入了未定义的领域:有时它有效,有时它不起作用。

    我不知道其他模块是否需要垫片,因为我不经常使用它们。如果我是你,我会检查他们是否打电话给define

    【讨论】:

    • 谢谢。这在逻辑上是有道理的,但是在从 shim 中删除 jquery、下划线和主干(所有这些都支持 AMD)之后,我在启动服务器时会遇到不同的错误,例如 Uncaught TypeError: object is not a function 试图创建主干模型或 $.ajaxSetup is not a function。每次我启动服务器时,错误都来自需求链中的不同脚本。所以看起来 jquery、下划线和主干现在没有被加载,即使我的第一个必需脚本的第一行是 require(['jquery', 'underscore', 'backbone', ...])
    【解决方案2】:

    您说“这些都不适用”,但是除非您手动修改下划线,否则它是certainly does

    【讨论】:

    • 但我没有在 HTML &lt;script&gt; 标记中手动加载下划线。我的requireJsConfig()函数调用require(["teacher"]),我的teacher.js文件的第一行是:require(['jquery', 'underscore', 'backbone', ...])
    猜你喜欢
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    • 2013-02-28
    • 2023-03-30
    相关资源
    最近更新 更多