【问题标题】:loading jquery plugins with require js使用 require js 加载 jquery 插件
【发布时间】:2013-01-23 06:35:00
【问题描述】:

我是新手,需要js,问题是我不太了解如何加载jQuery插件。

我想加载多个插件,但第一个插件已经出现问题,选择的插件

js

//site full url
var siteUrl = window.location.protocol+"//"+window.location.host + "/";

requirejs.config({
    baseUrl: siteUrl + "assets/js",

    paths: {
        "jquery": "libs/jquery",
        "jquery-ui": "libs/jquery-ui",
        "bootstrap": "libs/bootstrap",
        "scripts": "scripts",
        "plugins": "plugins",
    }, 
});

requirejs(['jquery', 'jquery-ui', 'bootstrap', 'plugins/chosen'],
function($, chosen){
    $('.chzn-select').chosen();
});

我的测试 html

<select data-placeholder="Choose a country..." style="width:350px;" class="chzn-select">
    <option value="">Test</option>
    <option value="">Test</option>
    <option value="">Test</option>
</select>

当我尝试加载它时,我收到以下错误

TypeError: $ is not a function


...tion(){"in"==self.hoverState&&self.show()},self.options.delay.show),void 0):self...

bootstrap.js (line 6)

TypeError: $(...).chosen is not a function


$('.chzn-select').chosen();

谁能指出我做错了什么?

【问题讨论】:

    标签: javascript jquery jquery-plugins requirejs


    【解决方案1】:

    您好,我想在这里告诉您,如果您想包含非 AMD 脚本(不包括 define() 调用),我们使用 shim config 。我想用一个简单的jquery高亮插件例子来解释一下。

    这将是您定义所有路径的配置文件

    paths:{
        "jquery":"/path/to/jquery",
        "jgHighlight":"/path/to/jquery.highlight"
    },
       shim:{
    
            deps:["jquery"], // jquery.highlight dependeps on jquery so it will load after jquery has been loaded 
            exports:"jqHighlight"
    
       }
    

    现在在以 define 开头的模块中,像这样包含 jqHighlight

    define(["requireModulesArray","jgHighlight"],function(requiredModulesAliasArray){
    
       // no need to include any alias for jgHighlight in function(...)
       //use it like this now
    
         $("#divT").highlight("someText");
    
    });
    

    类似的其他非 amd 模块也将被包含和使用。谢谢

    【讨论】:

      【解决方案2】:

      当您加载依赖项时,requirejs 会同时加载它们。当您收到该错误时,这意味着您的插件在加载 jQuery 之前已被加载和执行。您需要设置一个 shim 来告诉 requirejs 该插件依赖于已加载的 jQuery。

      此外,大多数 jQuery 插件不支持 AMD,因此您还需要告诉 requirejs 寻找什么来告诉它正确加载的脚本。您可以通过 shim 中的“导出”条目来执行此操作。

      我也不相信 jqueryUI 是 AMD 感知的,所以在 shim 中的一个条目可能也是为了这个。我不使用引导程序,所以我不确定你是否需要那里的任何东西。

      这是您的插件和 jQueryUI 的 shim,将其添加到您对 requirejs.config 的调用中:

      shim: {
          'plugins\chosen': {
              deps: [ 'jquery' ],
              exports: 'jQuery.fn.chosen'
          },
          'jquery-ui': {
              deps: [ 'jquery' ],
              exports: 'jQuery.ui'
          }
      }
      

      您可能还有一些我还没有发现的问题,但这至少应该能让您继续前进。此外,这可能值得一读:http://requirejs.org/docs/api.html#config-shim。如果您还没有阅读,我绝对会建议您阅读整页。

      【讨论】:

      • 感谢您的回复,尝试了同样的错误,我对此很迷茫,没有任何效果,我将放弃它,但感谢您的回复,编辑:现在一个新错误:TypeError: e.browser 未定义,nvm 还是谢谢
      • 我的错,我的插件确实出错了,谢谢你的帮助
      • 在使用这些插件时,define(['jquery','jquery-ui','jquery-foo'], function ($,$.ui,$.foo) {...}); 会是一个明智的主意吗?我们将如何使用这些功能?属性会自动添加到第一个 $ 吗?
      • @AthanClark 我不会说不明智,只是没必要。这些属性仍将照常添加到 jQuery 中。当加载 jQuery 插件(或任何其他修改现有对象的东西)时,定义“导出”属性的原因与其说是为了获取对它的引用,不如说是为了让 requirejs 可以验证模块是否正确加载。在元注释上 - 下一次,不要对旧的(近 2 年)答案发表评论,而是发布一个新问题并包含指向旧问题的链接以获取上下文。
      • “AMD 意识”是什么意思?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 2017-06-26
      • 2014-08-06
      • 2014-06-29
      相关资源
      最近更新 更多