是的,jQuery 将自己定义为“jquery”,全部小写。这很正常。
如果你打开 jQuery 的源代码,你会发现:
// Register as a named AMD module, since jQuery can be concatenated with other
// files that may use define, but not via 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( "jquery", [], function () { return jQuery; } );
}
所以你必须将其称为"jquery"
RequireJS 调用中随处可见。这里的问题是define
jQuery 使用的是"named define"这是我们在创建模块时通常不使用的东西。当我们运行 RequireJS 优化器时,它会为我们添加这些名称。
无论如何,当一个“被命名的define
" 用于将模块名称设置为给定的名称define
而不是通过文件名(当我们不使用命名时的情况)define
).
可以重命名"jquery"
to "jQuery"
, 像这样:
require.config({
baseUrl: "./js",
paths: {
"jquery": "jquery-1.10.2"
}
});
define("jQuery", ["jquery"], function ($) {
return $;
});
require(["jQuery"], function ($) {
console.log($);
console.log($("body")[0]);
});
我正在使用的版本define
将名称作为第一个参数。完整示例here.