javascript Requirejs:jQuery 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17017407/
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 06:47:43  来源:igfitidea点击:

Requirejs: jQuery is undefined

javascriptjqueryrequirejs

提问by Alex Beauchemin

Somehow I always receive this error:

不知何故,我总是收到此错误:

Uncaught ReferenceError: jQuery is not defined

未捕获的 ReferenceError:未定义 jQuery

I have the impression that jQuery (loaded via CDN) takes more time to load (confirmed by the network tab on Chrome). I run this locally on my PC, so that's why the CDN call will always be longer then the libraries. But isn't requirejs supposed to wait after jQuery is loaded before loading the other libraries?

我的印象是 jQuery(通过 CDN 加载)需要更多时间来加载(通过 Chrome 上的网络选项卡确认)。我在我的 PC 上本地运行它,所以这就是为什么 CDN 调用总是比库长的原因。但是,在加载其他库之前,requirejs 不是应该在加载 jQuery 之后等待吗?

My boot.js :

我的 boot.js :

(function(){
    requirejs.config({
        baseUrl: '/assets/js/',
        paths: {
            'lib': 'lib/',
            'src': 'src/',
            'jquery': [
                '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min',
                'lib/jquery-1.9.1.min'
            ],
            'mootools': 'lib/mootools-core-1.4.5',
            'class.mutators': 'lib/Class.Mutators.jQuery'
            //'order': 'assets/js/lib/order',
        },
        shim: {
            'class.mutators': {
                deps: [
                    'mootools'
                ],
                exports: 'classmutators'
            },
            'underscore': {
                    exports: '_'
            }
        },
        waitSeconds: 15
    });

    requirejs([
        'jquery',
        'src/app-require'
    ], function($){
        $(document).ready(function(){
            var App = new $.App($('body'));
        });
    });

})();

My app-require.js :

我的 app-require.js :

define([
    'mootools',
    'class.mutators',
    'src/Tracker',
    'lib/jquery.easing.1.3',
    'lib/nivo/jquery.nivo.slider.pack',
    'lib/isotope/jquery.isotope.min',
    'lib/waypoints.min'
], function() {

var className = 'App';
//--
return $[className] = new Class({
    jQuery: className,

    Implements: [Options, Events],

    options: {},

    //-- init
    //---------------------------------------------
    initialize: function(el, options) {
              ...
    },

            ...
    });
});

Any ideas?

有任何想法吗?

采纳答案by Antoine

jQuery is not in any dependencies. You should add it in the shim dependencies of mutator:

jQuery 没有任何依赖项。您应该将它添加到 mutator 的 shim 依赖项中:

  shim: {
            'class.mutators': {
                deps: [
                    'jquery',
                    'mootools'
                ],
                exports: 'classmutators'
            },
            'underscore': {
                    exports: '_'
            }
        },

Here is the official example of how to handle a jQuery dependency with shim: https://github.com/requirejs/example-jquery-shim#how-its-set-up

以下是如何使用 shim 处理 jQuery 依赖项的官方示例:https: //github.com/requirejs/example-jquery-shim#how-its-set-up