为什么不RequireJS传递到jQuery的功能别名吗?(Why isn't Requir

2019-07-28 22:51发布

我下载的样本项目最新释放RequireJS 。 他们的文档意味着加载的传入到相关功能的参数列表中任何东西(在相应的订单)。

所以我决定尝试一下...但它似乎没有工作!

  • Firebug (网片)显示jQuery的加载:所以RequireJS obvioulsy这样做,部分成功
  • Firebug (控制台选项卡)显示“$不是一个函数”

我的问题是:为什么没有得到填充别名?

我的代码如下所示:

<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>

THIER样例如:

//Inside scripts/main.js
require(["some/module", "a.js", "b.js"], function(someModule) {
    //...
});

Answer 1:

jQuery的应该通过专门的名字“jQuery的”载,否则它不会注册本身(因为jQuery使用一个名为定义)。

// 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($);
});

这就是为什么命名的主要原因定义被认为是一种反模式,并在必要时应该只用于(当你有同一个文件内的多个模块)。



Answer 2:

Be sure to read the "README.md" for the RequireJS+jQuery sample project. There are many complications with using jQuery that you need to decide on how best to tackle it for your project during the initial setup. Once you've figured out what's best for you and implemented it, though, it shouldn't be a problem again.

Much of the complication also comes from the fact that jQuery isn't a true AMD module, they just have a hack in the codebase to do a define if it detects the define function is available. For example, this means the jQuery module name will always be "jquery" (note the lowercase 'q') unless you wrap it yourself, so if you define the path for it in your config you MUST have the key named "jquery" (again, with a lowercase 'q') or there will be a mismatch. That bit us when first setting up RequireJS on our project (we named the key "jQuery").



Answer 3:

是你的脚本文件夹“脚本”或“脚本”?

你正在做的“脚本/ require.js”以及为“脚本/ jQuery的/核心/ jQuery的1.7.2.min”的请求。 看看萤火虫的网片......我打赌你生成一个404那里。



文章来源: Why isn't RequireJS passing jQuery into the functions alias?