为什么RequireJS不将jQuery传递给函数别名? [英] Why isn't RequireJS passing jQuery into the functions alias?
问题描述
我为 示例项目 :http://requirejs.org/docs/download.html#latest> 最新发布 RequireJS
强> 。他们的 文档暗示 任何加载的内容都会传递到相关函数的参数列表中(按相应的顺序)。
I downloaded the sample-project for the latest-release of RequireJS
. Their documentation implies anything loaded is passed-into the parameter list of the associated function (in corresponding order).
所以我决定尝试一下......但它似乎不起作用!
So I decided to try it...but it doesn't seem to work!
-
Firebug
(网络标签)显示jQuery
正在加载:所以RequireJS
obvioulsy成功完成该部分 -
Firebug
(控制台标签)显示'$不是函数'
Firebug
(net tab) showsjQuery
as being loaded: soRequireJS
obvioulsy did that part successfullyFirebug
(console tab) shows '$ is not a function'
我的问题是:为什么不填充别名?
我的代码看起来像:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/require.js" type="text/javascript"></script>
<script type="text/javascript">
require(["scripts/jQuery/Core/jquery-1.7.2.min"], function ($) {
// jQuery is not passed-into the function, so the alias fails!
$(function () {
var stop = "";
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
更多样本看起来像:
//Inside scripts/main.js
require(["some/module", "a.js", "b.js"], function(someModule) {
//...
});
推荐答案
jQuery应该通过特殊名称jquery加载,否则它不会自己注册(因为jQuery使用命名的定义)。
jQuery should be loaded through the special name "jquery", otherwise it won't register itself (since jQuery uses a named define).
// create an alias that points to proper file
require.config({
paths : {
jquery : "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min"
}
});
// require jquery usign the path config
require(["jquery"], function ($) {
console.log($);
});
这就是为什么命名的define被认为是反模式的主要原因,应该只在需要时使用(当你在同一个文件中有多个模块时)。
That is the main reasons why named define is considered an anti-pattern and should be used only when needed (when you have multiple modules inside same file).
这篇关于为什么RequireJS不将jQuery传递给函数别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!