使用Npm模块时的jQuery插件

使用Npm模块时的jQuery插件

本文介绍了使用Npm模块时的jQuery插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我是Npm包的新手(来自Ruby),并尝试加载不一定在NPM上的jQuery插件.我正在构建一个Chrome扩展程序.下面的代码正在注入浏览器的content.js脚本中使用.因此,需要使用格式和日期选择器之类的组件.

I'm new to Npm packages (coming from Ruby) and trying to load a jQuery plugin that's not necessarily on NPM. I'm building a Chrome extension. The code below is being used in the content.js script that's being injected in to the browser. 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")($);

那也不起作用.

我显然不知道自己在做什么,但是希望这比看起来容易.

I clearly have no idea what I'm doing, but hoping this is easier than it appears.

谢谢!

推荐答案

我遇到了同样的问题,并且这样做:

I had the same issue and did this:

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

使$和jQuery全局满足我像这样简单地使用的插件:

Making $ and jQuery global satisfies plugins that I simply use like this:

require('./jquery-datepicker');

在所有其他情况下,我都避免使用全局变量,但在jQuery的情况下,我认为如果您不这样做的话,您会遇到麻烦.如果您有兴趣,我可以使用browserify在客户端使用节点样式模块.

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. In case you are interested, I use browserify to use node style modules client side.

这篇关于使用Npm模块时的jQuery插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 13:41