Javascript jQuery / Dojo - 如何将 jQuery 与 Dojo 工具包一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2850314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:10:04  来源:igfitidea点击:

jQuery / Dojo - How do I use jQuery with Dojo toolkit

javascriptjquerydojo

提问by John Himmelman

How do I use jQuery with Dojo toolkit? I've heard of both libraries being used simultaneously, jQuery for DOM-related and Dojo for UI (dijit), but I can't find any tutorials or examples of this. Will I run into any conflicts or issues if I load both libraries?

如何将 jQuery 与 Dojo 工具包一起使用?我听说过同时使用这两个库,jQuery 用于 DOM 相关,Dojo 用于 UI (dijit),但我找不到任何教程或示例。如果同时加载这两个库,我会遇到任何冲突或问题吗?

采纳答案by Eric LaForce

You can use them beside each other with no issues because Dojo does not override $ like some other javascript libraries.

您可以毫无问题地将它们并排使用,因为 Dojo 不会像其他一些 javascript 库那样覆盖 $。

回答by Jeremy

You can use jQuery by pulling it into your app via a script tag in the head of your website, there will be no conflicts with dojo.

您可以通过网站头部的脚本标签将 jQuery 拉入您的应用程序来使用它,不会与 dojo 发生冲突。

However something to keep in mind when using jQuery with dojo, especially with dojo version 1.8and its full AMD support. It is cleaner (especially if you can't pull in jQuery in the head of your website) to take advantage of AMD (asynchronous module definition). You will need to make a package entry within the dojo config script to correctly pull in the framework. Here is an example that uses a library location for jquery and jquery-ui...

但是在将 jQuery 与 dojo 一起使用时要记住一些事情,尤其是在dojo 1.8 版及其完全支持 AMD 的情况下。利用 AMD(异步模块定义)更简洁(尤其是当您不能在网站的头部引入jQuery 时)。您将需要在 dojo 配置脚本中创建一个包条目以正确拉入框架。这是一个使用 jquery 和 jquery-ui 的库位置的示例...

<!-- external library configuration code included in header to make sure this
    is loaded before code in body-->
    <!-- dojo config -->
    <script>
            /* Instead of using the inline dojo-config attribute
            * create this variable so we can configure dojo here.
            * This seems a little clearer, easier to read as a config.
            */
            var dojoConfig = {
                baseUrl: "./",
                async: true,
                isDebug: true,
                parseOnLoad: false,//false to allow for us to call this independantly in js later on

                //here are the packages dojo will be aware of and related js files
                packages: [
                    //dojo specific packages
                    {name: "dojo", location: "libs/dojo"},
                    {name: "dijit", location: "libs/dijit"},
                    {name: "dojox", location: "libs/dojox"},
                    {name: "jquery", location: "libs/jquery", main: "jquery-1.8.2"},
                    {name: "jqueryui", location: "libs/jquery", main: "jquery-ui-1.9.1"},
                ]

            };


    </script>

My folder structure just has a libs folder at the root, which is why I have "./" for the base url, but you could just as easily pull from a cdn location.

我的文件夹结构在根目录只有一个 libs 文件夹,这就是为什么我在基本 url 中有“./”,但你也可以很容易地从 cdn location 中拉取

Without this config entry jQuery will not function as expected, and you may end up getting "is not a function" errors popping up in the console.

如果没有此配置条目,jQuery 将无法按预期运行,并且您最终可能会在控制台中弹出“不是函数”错误。

If you do put a separate script tag in to pull in jQuery or other third party framework and also use AMD to do the same, you'll just end up pulling it in a second time when you requireit for dojo for the first time.

如果您确实将单独的脚本标记放入 jQuery 或其他第三方框架并使用 AMD 执行相同操作,那么当您第一次需要它用于 dojo时,您将最终第二次拉取它。

回答by Leftium

You can use Dojo's AMD loader to load jQuery.

您可以使用 Dojo 的 AMD 加载器来加载 jQuery。

The following snippet even aliases $to dojo.queryand still uses jQuery without conflict (I don't recommend it, though!):

下面的代码片段甚至是jQuery 的别名$dojo.query并且仍然使用 jQuery 而不会发生冲突(不过我不推荐它!):

  define.amd.jQuery = true;

  require(["jquery", "dojo/query", "dojo/NodeList-dom"],
  function(jquery, $) {
    $("output").style("visibility", "visible");     // using Dojo
    jquery("#output").css("visibility", "hidden");  // using jQuery
  });

Full explanation and source code: Loading jQuery with Dojo 1.7 AMD loader

完整解释和源代码:Loading jQuery with Dojo 1.7 AMD loader

回答by yoda

You can namespace jQuery, for example, in order to avoid conflicts.

例如,您可以命名 jQuery,以避免冲突。

check http://docs.jquery.com/Using_jQuery_with_Other_Libraries

检查http://docs.jquery.com/Using_jQuery_with_Other_Libraries

回答by John Slegers

jQuery supports AMD for quite a while now.

jQuery 支持 AMD 已经有一段时间了。

You can use the "paths" property of your ADM config to tell your AMD loader where to find jQuery :

您可以使用 ADM 配置的“paths”属性来告诉您的 AMD 加载器在哪里可以找到 jQuery :

var amdconfig = {
  baseUrl: __AMD_CONFIG_BASE_URL__,
  packages: [
    {name: "dojo", location: "./lib/dojo"},
    {name: "app", location: "./app"}
  ],
  paths: {
    jquery: "./lib/jquery/jquery-1.12.4"
  }
};

Then, you can import jQuery into your ADM modules the same way you import any other AMD module, and use it alongside your Dojo modules:

然后,您可以像导入任何其他 AMD 模块一样将 jQuery 导入您的 ADM 模块,并将其与您的 Dojo 模块一起使用:

define([
  "dojo/dom",
  "jquery"
], function(
  dom,
  $
) {

  ...

});


Note

笔记

You can use the same config for AMD projects that use RequireJS instead of Dojo :

您可以对使用 RequireJS 而不是 Dojo 的 AMD 项目使用相同的配置:

if (require.config) {
  // RequireJS
  require.config(amdconfig);
} else {
  // Dojo
  require(amdconfig);
}