RequireJS-jQuery是否区分大小写? [英] RequireJS - is jQuery case sensitive?
本文介绍了RequireJS-jQuery是否区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试开始使用RequireJS,但遇到了一个恼人的问题。。。
require.config({
baseUrl: 'app_content/scripts',
paths: {
// the left side is the module ID,
// the right side is the path to
// the jQuery file, relative to baseUrl.
// Also, the path should NOT include
// the '.js' file extension. This example
// is using jQuery 1.9.0 located at
// js/lib/jquery-1.9.0.js, relative to
// the HTML page.
'jQuery': 'lib/jQuery/jQuery-2.0.3'
}
});
这个使用大写的Q
不起作用,但是如果我将其改为jquery
,它就起作用了。另外,在我的必填区域中也是如此.
<script type="text/javascript">
require(["jQuery"], function ($) {
console.log($);
});
</script>
这将返回undefined
,但是如果我将所有内容都更改为向上jquery
,则可以正常工作。
这是预期的行为吗?我能为此做些什么吗?
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; } );
}
所以您必须在RequireJS调用中的任何地方都将其称为"jquery"
。这里的问题是jQuery使用的define
是一个"named define
",这是我们在创建模块时通常不使用的。当我们运行RequireJS优化器时,它会为我们添加这些名称。
无论如何,当使用"命名的define
"时,模块名称被设置为指定给define
的名称,而不是通过文件名(与我们不使用命名的define
时的情况不同)。
可以将"jquery"
重命名为"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。
这篇关于RequireJS-jQuery是否区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文