在 RequireJS 中 - 无法在路径中为 jQuery 名称添加别名
我是一个 RequireJS 菜鸟。当我使用“require.config”并包含名称与 jQuery 不同的 jQuery 路径时,结果不符合预期。
这是一个非常简单的示例,可以帮助解释我的问题。
文件结构
root
├── Index.htm
└── scripts
├── libs
│ ├── jquery-1.7.1.js
│ └── require.js
├── main.js
└── someModule.js
index.htm
<html>
<head>
<title>BackboneJS Modular app with RequireJS</title>
<script data-main="scripts/main" src="scripts/libs/require.js"></script>
</head>
<body>
<h3>BackboneJS is awesome</h3>
</body>
</html>
这里的脚本标签引用需要在脚本/库中。当 require 运行时,脚本目录中名为 main.js 的 JavaScript 文件应该被执行。
main.js
require.config({
"paths": {
"mod1": "someModule"
}
});
require(["mod1"], function (sm) {
console.log(sm.someValue);
});
根据我的经验,“mod1”可以是任何内容,只要它在 require.config 路径和 require 方法中引用相同即可。
someModule.js
define([], function () {
console.log();
return { someValue: "abcd" };
});
只是为了完整起见,我包含了 someModule.js
当我包含 JQuery 时,会出现明显的不一致情况。
在下面的 main.js 中,我将 jQuery 添加到配置和 require 方法中。
Main.js
require.config({
"paths": {
"jquery": "libs/jquery-1.7.1"
,"mod1": "someModule"
}
});
require(["mod1", "jquery"], function (sm, $) {
console.log(sm.someValue);
console.log($);
});
通过添加 jQuery,一切似乎仍然有效。 “console.log($)”写入 jQuery 函数。
现在是踢球者。在下面的代码中,我将两个路径中的“jquery”更改为“jqueryA”,并要求
require.config({
"paths": {
"jqueryA": "libs/jquery-1.7.1"
,"mod1": "someModule"
}
});
require(["mod1", "jqueryA"], function (sm, $) {
console.log(sm.someValue);
console.log($);
});
现在“console.log($)”写入 null。
这是应该预料到的吗?有没有理由为什么名称必须是 jquery,但对于 mod1 来说它可以是任何东西?
我可以毫无问题地解决这个问题,但这个问题看起来很奇怪。我知道我可以使用组合的 RequireJS 和 jQuery 文件,但是当 jQuery 有更新时,我不想依赖 RequireJS 来包含新的 jQuery。
I'm a RequireJS noob. When I use "require.config" and include a path to jQuery with a name different than jQuery, results are not as expected.
Here's a very simple example to help explain my issue.
Structure of files
root
├── Index.htm
└── scripts
├── libs
│ ├── jquery-1.7.1.js
│ └── require.js
├── main.js
└── someModule.js
index.htm
<html>
<head>
<title>BackboneJS Modular app with RequireJS</title>
<script data-main="scripts/main" src="scripts/libs/require.js"></script>
</head>
<body>
<h3>BackboneJS is awesome</h3>
</body>
</html>
Here the script tag references require in scripts/libs. When require gets ran the JavaScript file called main.js in the scripts directory should be executed.
main.js
require.config({
"paths": {
"mod1": "someModule"
}
});
require(["mod1"], function (sm) {
console.log(sm.someValue);
});
In my experience the "mod1" can be anything as long as it's referenced the same in the require.config path and in the require method.
someModule.js
define([], function () {
console.log();
return { someValue: "abcd" };
});
Just for completeness I included someModule.js
The perceived inconstancy occurs when I include JQuery.
In the following main.js I added jQuery to the config and the require method.
Main.js
require.config({
"paths": {
"jquery": "libs/jquery-1.7.1"
,"mod1": "someModule"
}
});
require(["mod1", "jquery"], function (sm, $) {
console.log(sm.someValue);
console.log($);
});
With the additional of jQuery everything seems to still works. The "console.log($)" writes the jQuery function.
Now the kicker. In the following code I change "jquery" to "jqueryA" in both the paths and require
require.config({
"paths": {
"jqueryA": "libs/jquery-1.7.1"
,"mod1": "someModule"
}
});
require(["mod1", "jqueryA"], function (sm, $) {
console.log(sm.someValue);
console.log($);
});
Now "console.log($)" writes null.
Should this be expected? Is there a reason why the name must be jquery, but for mod1 it can be anything?
I can work-around this without a problem, but this issue seems odd. I know I can use the combined RequireJS and jQuery file, but when jQuery has an update I don't want to be dependent on RequireJS to include the new jQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 jQuery 1.7 中,他们决定支持 AMD 加载。为此,它定义了一个名为“jquery”的模块,该模块传回对 jQuery 对象的引用。当您使用其他名称(例如“jqueryA”)定义 jquery 的路径时,事情并不完全像您想象的那样。
jquery 脚本始终将自身定义为名为“jquery”的模块,该模块已通过应用程序的 require 注册。当您将路径快捷方式命名为“jquery”并且“jquery”作为依赖项加载时,require 实际上引用了 jquery-1.7.1.js 定义的“jquery”模块,该模块确实传回了正确的引用。当您将模块快捷方式命名为 jqueryA 时,您现在引用的是未定义的引用,因为 jquery 脚本本身不会传回引用,除非通过名为“jquery”的模块。我知道这很愚蠢。
jquery 脚本将该模块定义为“jquery”,并希望您将其简单地引用为“jquery”。如果您想将其引用为另一个名称(并且作为奖励,防止它与其他加载的 jquery 库发生冲突),请使用此方法:
使用 requirejs 和 jquery,而不破坏全局 jquery?
In jQuery 1.7 they decided to support AMD loading. To do this, it defines a module named 'jquery' which passes back a reference to the jQuery object. When you define your path to jquery with another name (eg 'jqueryA'), things aren't exactly breaking as you think they are.
The jquery script always defines itself as a module named 'jquery', which is registered with require for your app. When you named your path shortcut 'jquery' and 'jquery' was loaded as a dependency, require was actually referencing the 'jquery' module defined by jquery-1.7.1.js, which does pass back the correct reference. When you name your module shortcut jqueryA, you are now referencing an undefined reference, because the jquery script itself does not pass back a reference, except via the module named 'jquery'. It's silly, I know.
The jquery script defines the module as 'jquery' and expects that you will simply reference it as 'jquery'. If you want to reference it as another name (and as a bonus, keep it from conflicting with other loaded jquery libraries), use this method:
Use requirejs and jquery, without clobbering global jquery?
这是我的解决方法,基于我读到的 Require.JS 2.1.0 的实现:
Here's my workaround, based on the implementation I read of Require.JS 2.1.0:
我相信我找到了问题的答案。
可以选择调用 AMD Define() 来注册模块
https://github.com/documentcloud/underscore/pull/338#issuecomment-3253751
这是上一个链接的引用。尽管它与下划线有关,但我相信它也与 JQuery 有关。
这里的咆哮很好地描述了 AMD 和命名模块的问题。
具有命名定义的 AMD 模块。这么多的痛苦,换来的是什么?
http://dvdotsenko.blogspot.com /2011/12/amd-modules-with-named-defines-so-much.html
引用上面的链接
在这篇文章中,James Burk 建议不要使用名称模块。
https://github.com/jrburke/requirejs/wiki/Updating -现有库#wiki-anon
异常糟糕。例外让菜鸟变得很困难。
I believe I found the answer to my issue.
Optionally call AMD define() to register module
https://github.com/documentcloud/underscore/pull/338#issuecomment-3253751
Here's a quote from the previous link. Even though it pertains to underscore, I believe it relates to JQuery also.
Here's a rant that does a very good job of describing the issues with AMD and named modules.
AMD modules with named defines. So much pain for what gain?
http://dvdotsenko.blogspot.com/2011/12/amd-modules-with-named-defines-so-much.html
Quote from the link above
In this post James Burk recommends not using name module.
https://github.com/jrburke/requirejs/wiki/Updating-existing-libraries#wiki-anon
Exception suck. Exceptions makes it difficult for noobs.