简体   繁体   English

使用 jQuery 插件浏览器

[英]Browserify with jQuery plugins

I'm trying to get some jQuery plugins to work with browserify.我正在尝试让一些 jQuery 插件与 browserify 一起使用。 I have my package.json setup like this:我的 package.json 设置如下:

  "browser": {
    "jquery": "./client/js/vendors/jquery-2.2.2.min.js",
    "jquery-validation": "./client/js/vendors/jquery.validate.js"
  },
  "browserify-shim": {
    "jquery": "global:$"
  },

However, when I require('jquery-validation') , I get cannot read property fn of undefined as it relates to this plugin.但是,当我require('jquery-validation') ,我无法读取 undefined 的属性fn ,因为它与此插件相关。 I'm trying to also have it so that $ will be global as it's used all over, without having to require it.我也试图拥有它,以便$将是全局的,因为它被到处使用,而不必要求它。

I've seen so many different articles and configs for this, but nothing seems to work.我已经看过很多不同的文章和配置,但似乎没有任何效果。

Any suggestions or clarity would be greatly appreciated.任何建议或清晰度将不胜感激。

EDIT:编辑:

I also sometimes get Uncaught Error: Cannot find module 'jquery'我有时也会收到Uncaught Error: Cannot find module 'jquery'

  1. You don't need to shim jquery, it's conpatible with node yoo can simply install it with npm install jquery --save and make var $= require("jquery") in your code.您不需要填充 jquery,它与节点 yoo 兼容,只需使用npm install jquery --save安装它并在代码中 make var $= require("jquery")
  2. Yo do need to shim jquery.validate.哟确实需要填充jquery.validate。

package.json:包.json:

{  
   "browser":{  
      "jquery-validation":"./node_modules/jquery-validation/dist/jquery.validate.js"
   },
   "browserify-shim":{  
      "jquery-validation":{  
         "depends":[  
            "jquery:jQuery"
         ]
      }
   },
   "browserify":{  
      "transform":[  
         "browserify-shim"
      ]
   },
   "dependencies":{  
      "jquery":"^2.0.0",
      "jquery-validation":"^1.15.1"
   }
}

Note: jquery.validate depend on jQuery version 2.0^ ( you can see in the package.json file of the jquery-validation's npm package ) so you have to set dependency on jquery ^2.0 in your project otherwise jquery-validation will load he's own version of jQuery and integration will not work.注意:jquery.validate 依赖于 jQuery 版本 2.0^(你可以在 jquery-validation 的 npm 包的 package.json 文件中看到)所以你必须在你的项目中设置对 jquery ^2.0 的依赖,否则 jquery-validation 会加载他自己的jQuery 版本和集成将不起作用。

看起来你有错字,你应该调用require('jquery-validation')而不是require('jquery-validate')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM