Javascript 使用require.js实现jQuery的正确方法
我正在使用require.js和jQuery的最新稳定版本,目前我正在使用像这样的jQueryJavascript 使用require.js实现jQuery的正确方法,javascript,jquery,requirejs,amd,Javascript,Jquery,Requirejs,Amd,我正在使用require.js和jQuery的最新稳定版本,目前我正在使用像这样的jQuery requirejs.config({ paths: { 'jQuery': 'vendor/jquery', } }); require(['jQuery'], function(jQuery) { log(jQuery); // working }); 我不明白的是,我真的不需要显式地返回jQuery,因为这仍然可以工作(在其他模块中也可以): 现在我不确定这是否是正确的方法,还
requirejs.config({
paths: {
'jQuery': 'vendor/jquery',
}
});
require(['jQuery'], function(jQuery) {
log(jQuery); // working
});
我不明白的是,我真的不需要显式地返回jQuery,因为这仍然可以工作(在其他模块中也可以):
现在我不确定这是否是正确的方法,还因为使用$dollar符号来引用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
});
// 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; } );
}
更新:已更新以反映上述信息。有关包括优化器的逐步步骤,请参见。我只想再次强调,这种无冲突的方法只有在所有插件都兼容AMD的情况下才有效。如果您使用的是支持AMD的jQuery的最新版本,您可以点击以下链接:我做了这个,我的大部分脚本都工作了,但在某些情况下,$.fn是未定义的,尽管$看起来还可以。。。知道为什么吗?
// 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; } );
}