javascript - 使用 require.js 实现 jQuery 的正确方法

标签 javascript jquery requirejs amd

我正在使用 require.js 和 jQuery 的当前稳定版本,并且我目前包括这样的 jQuery

requirejs.config({
paths: {
    'jQuery': 'vendor/jquery',
}
});

require(['jQuery'], function(jQuery) {
    log(jQuery); // working
});

我没有得到的是我真的不需要显式地返回 jQuery,因为它仍然可以工作(也在其他模块中):

require(['jQuery'], function( // nothing here ) {
    log(jQuery); // working
});

现在我不确定这是否是正确的做法,还因为使用 $ 美元符号引用 jQuery 不起作用!

最佳答案

我看到的重点:

  1. jQuery 在与 RequireJS 一起使用时将自己注册为名为“jquery”的模块(全部小写)。在您的示例中,您试图将它用作“jQuery”,这有点让人困惑,因为这也是它在加载时注册的全局函数的名称(参见第 2 点)。
  2. 默认情况下,jQuery 使用全局函数“$”和“jQuery”注册自己,即使与 AMD/RequireJS 一起使用时也是如此。如果您想关闭此行为,您必须调用 noConflict 函数。
  3. 您可以在 noConflict 调用中包装对 jQuery 的 RequireJS 引用,如下例所示。据我所知,当您没有其他需要全局 $ 或 jQuery 的模块时,这是推荐的方法:

    requirejs.config({
        paths: {
            'jquery': 'vendor/jquery',
        }
    });
    
    define('jquery-private', ['jquery'], function (jq) {
        return jq.noConflict( true );
    });
    
    require(['jquery-private'], function(jq) {
        console.log(jq);      // working
        console.log($);       // undefined
        console.log(jQuery);  // undefined
    });
    

另见 my answer in this question关于如何映射其他模块以使用私有(private)(无冲突)版本。

有关更多背景信息,请参阅 jQuery 源代码中的这些行,它们是我上面描述的所有内容的原因:

    // Expose jQuery to the global object
    window.jQuery = window.$ = jQuery;

    // Expose jQuery as an AMD module, but only for AMD loaders that
    // understand the issues with loading multiple versions of jQuery
    // in a page that all might call define(). The loader will indicate
    // they have special allowances for multiple jQuery versions by
    // specifying define.amd.jQuery = true. Register as a named module,
    // since jQuery can be concatenated with other files that may use define,
    // but not use a proper concatenation script that understands anonymous
    // AMD modules. A named AMD is safest and most robust way to register.
    // Lowercase jquery is used because AMD module names are derived from
    // file names, and jQuery is normally delivered in a lowercase file name.
    // Do this after creating the global so that if an AMD module wants to call
    // noConflict to hide this version of jQuery, it will work.
    if ( typeof define === "function" && define.amd && define.amd.jQuery ) {
        define( "jquery", [], function () { return jQuery; } );
    }

更新 Use with jQuery section of the RequireJS site已更新以反射(reflect)上述信息。另见 this answer逐步包括优化器。只是想再次强调,只有当您的所有插件都与 AMD 兼容时,这种 noConflict 方法才有效。

关于javascript - 使用 require.js 实现 jQuery 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15613577/

相关文章:

php - 想要显示ajax加载gif

jquery - 将窗口中当前聚焦的行居中?

jquery - 如何设置backbone fetch回调方法

javascript - Angular 鼠标事件仅在 Firefox 上停止工作

javascript - RequireJS 中本地 require 和全局 require 的区别

javascript - JSON 文件的 WebGl 渲染非常慢,是因为 JSON 文件的大小吗?

javascript - 覆盖 div 不会阻止在特定时间窗口中触发的下一个事件

javascript - 更改 DIV 上的背景颜色 - 使用选项卡交换颜色

javascript - 将数据插入数据库。 Cakephp、AJAX、jQuery、异步

javascript - 使用 jQuery 在屏幕上创建 NxN 网格