简体   繁体   English

使用Npm模块时的jQuery插件

[英]jQuery plugin when using Npm modules

I'm new to Npm packages (coming from Ruby) and trying to load a jQuery plugin that's not necessarily on NPM. 我是Npm软件包(来自Ruby)的新手,并试图加载一个不一定在NPM上的jQuery插件。 I'm building a Chrome extension. 我正在构建Chrome扩展程序。 The code below is being used in the content.js script that's being injected in to the browser. 下面的代码正在被注入浏览器的content.js脚本中使用。 So components like formatting and date pickers are desirable. 因此,格式化和日期选择器等组件是可取的。

At the top of my app file, i have the following: 在我的应用程序文件的顶部,我有以下内容:

var React = require("react");
var $ = require("jquery");
var moment = require("moment");

All works fine, but now I want to add a plugin, and I'm not quite sure where to put it and how to get it accessible to the app. 一切正常,但现在我想添加一个插件,我不太确定在哪里放它以及如何让它可以访问应用程序。 I've tried just loading it like: 我试过加载它就像:

var React = require("react");
var $ = require("jquery");
require("./jquery-datepicker");
var moment = require("moment");

That doesn't work becausae it can't find $ in the script. 这不起作用因为它无法在脚本中找到$ So then I tried: 那么我试过:

require("./jquery-datepicker")($);

That didn't work either. 那也行不通。

I clearly have no idea what I'm doing, but hoping this is easier than it appears. 我显然不知道我在做什么,但希望这比它看起来更容易。

Thanks! 谢谢!

I had the same issue and did this: 我有同样的问题,并做了这个:

window.jQuery = require('jquery');
window.$ = window.jQuery;

Making $ and jQuery global satisfies plugins that I simply use like this: 使$和jQuery全局满足我简单使用的插件:

require('./jquery-datepicker');

In all other cases I avoid globals, but in the case of jQuery I think you are going against the grain if you don't. 在所有其他情况下,我避免使用全局变量,但在jQuery的情况下,如果你不这样做,我认为你会反对谷歌。 In case you are interested, I use browserify to use node style modules client side. 如果您有兴趣,我使用browserify来使用节点样式模块客户端。

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

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