使用npm install导入jQuery-ui和jQuery [英] import jQuery-ui and jQuery with npm install

查看:810
本文介绍了使用npm install导入jQuery-ui和jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

npm的新功能在这里.

Somewhat new to npm here..

我想在我的项目中使用jQuery-ui.我曾经在HTML中使用脚本标签导入jQuery和jQuery-ui,但是我想同时使用npm install导入.

I'd like to use jQuery-ui in my project. I'm used to importing both jQuery and jQuery-ui with a script tag in my HTML, but I would like to import both using npm install.

我可以使用jQuery.

I got jQuery working with.

import $ from 'jquery'

但是每当我尝试导入jquery-ui时,都会出现此错误:

But whenever I try to import jquery-ui I get this error:

未捕获的ReferenceError:jQuery未定义

Uncaught ReferenceError: jQuery is not defined

关于此错误的帖子很多.但是它们似乎都集中在人们周围,只是以错误的顺序放置了jQuery和jQuery-ui的脚本标签.

There's a lot of posts about this error. But all of them seem to be centered around people just putting their script tags for jQuery and jQuery-ui in the wrong order.

我找不到有关如何通过npm install同时安装jQuery和jQuery-ui的任何信息?

I can't find anything on how to install both jQuery and jQuery-ui with npm install?

有什么想法吗?

推荐答案

只需尝试将 jquery (更新为3.1.0)& jquery-ui (至1.12.0),并得到了完全相同的错误.

Just tried updating jquery (to 3.1.0) & jquery-ui (to 1.12.0) and got the very same error.

jquery-ui 的较新版本似乎需要全局jQuery变量进行初始化,或者 jquery 的较新版本默认情况下未设置jQuery全局变量,因此 Uncaught ReferenceError .

Newer versions of jquery-ui seems to require a global jQuery variable to initialize or newer versions of jquery does not set the jQuery global variable by default anymore, hence the Uncaught ReferenceError.

一个明确的解决方案是在导入 jquery-ui 之前设置global.jQuery = require('jquery').

A clear solution is to set global.jQuery = require('jquery') before importing jquery-ui.

它似乎不适用于 browserify ,因为 browserify 优先于其他表达式进行导入(导入被放置在浏览器代码的顶部,即使它们被放置在浏览器代码的后面)原始代码中的其他表达式).

It does not seem to work for browserify though, since browserify prioritizes imports over other expressions (imports are placed on top of the browserified code even if they were placed behind other expressions in the original code).

因此,如果您使用的是 browserify ,请尝试 jquery @ 2.2.4和 jquery-ui @ 1.10.5,然后将其导入为:

So if you're using browserify, try jquery@2.2.4 and jquery-ui@1.10.5, then import as:

import $ from 'jquery';
import 'jquery-ui';

为我工作.

这篇关于使用npm install导入jQuery-ui和jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆