ES6 + jQuery + Bootstrap - 未捕获的 ReferenceError:jQuery 未定义?
如何在 ES6 中将 jQuery 导入为 bootstrap 的依赖项?
我尝试过:
import {$,jQuery} from 'jquery';
import bootstrap from 'bootstrap';
但我总是收到此错误:
transition.js:59 Uncaught ReferenceError: jQuery is not defined
指向此文件:
/* ========================================================================
* Bootstrap: transition.js v3.3.7
* http://getbootstrap.com/javascript/#transitions
* ========================================================================
* Copyright 2011-2016 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// CSS TRANSITION SUPPORT (Shoutout: http://www.modernizr.com/)
// ============================================================
function transitionEnd() {
var el = document.createElement('bootstrap')
var transEndEventNames = {
WebkitTransition : 'webkitTransitionEnd',
MozTransition : 'transitionend',
OTransition : 'oTransitionEnd otransitionend',
transition : 'transitionend'
}
for (var name in transEndEventNames) {
if (el.style[name] !== undefined) {
return { end: transEndEventNames[name] }
}
}
return false // explicit for ie8 ( ._.)
}
// http://blog.alexmaccaw.com/css-transitions
$.fn.emulateTransitionEnd = function (duration) {
var called = false
var $el = this
$(this).one('bsTransitionEnd', function () { called = true })
var callback = function () { if (!called) $($el).trigger($.support.transition.end) }
setTimeout(callback, duration)
return this
}
$(function () {
$.support.transition = transitionEnd()
if (!$.support.transition) return
$.event.special.bsTransitionEnd = {
bindType: $.support.transition.end,
delegateType: $.support.transition.end,
handle: function (e) {
if ($(e.target).is(this)) return e.handleObj.handler.apply(this, arguments)
}
}
})
}(jQuery);
有什么想法吗?
编辑:
我的 gulp 文件:
var gulp = require('gulp');
var browserify = require('browserify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
var gutil = require('gulp-util');
gulp.task('es6', function() {
browserify({
entries: 'js/app.js',
debug: true
})
.transform(babelify, { presets: ['es2015'] })
.on('error',gutil.log)
.bundle()
.on('error',gutil.log)
.pipe(source('compile.js'))
.pipe(gulp.dest('js'));
});
gulp.task('default', ['es6']);
编辑 2:
package.json:
{
"name": "es6",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-preset-es2015": "^6.16.0",
"babelify": "^7.3.0",
"browserify": "^13.1.0",
"gulp": "^3.9.1",
"gulp-uglify": "^2.0.0",
"gulp-util": "^3.0.7",
"pump": "^1.0.1",
"vinyl-source-stream": "^1.1.0"
},
"browser": {
"jquery": "./node_modules/jquery/dist/jquery.js",
},
"browserify-shim": {
"jquery": "$", // or change it to jQuery
},
"browserify": {
"transform": [
"browserify-shim"
]
}
}
错误:
Starting 'build'... events.js:160 throw er; // Unhandled 'error' event ^
Error: SyntaxError: Unexpected token } in JSON at position 526 while parsing json file
我试过这个 答案 并且它有效。
// webpack.config.js
module.exports = {
...
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})
]
};
一般来说,您在
import
中执行的解构是不对的,因为
$
或
jQuery
对象是主要对象:
import $ from 'jquery';
// or
import jQuery from 'jquery'
就您而言,您正在处理一个模块 (
boostrap-transition
),它确实希望在全局范围内使用 jQuery。
不久前,我也在使用
materialize
模块时遇到过类似的问题。
如果您使用的是
webpack
,则可以按照 @galkowskit 的回答步骤操作。
如果您使用的是
browserify
,则需要执行
browserify 转换
,如下所示:
"browser": {
"jquery": "./node_modules/jquery/dist/jquery.js",
},
"browserify-shim": {
"jquery": "$", // or change it to jQuery
},
"browserify": {
"transform": [
"browserify-shim"
]
}
您可以将其放入
package.json
文件中:当 Gulp 要调用
browserify
时,它将读取您的
package.json
以获取配置提示并为 jQuery 执行此填充程序。
在 Webpack 中我通常使用(
webpack.config.js
):
externals: {
jquery: "jQuery"
}
然后:
import jQuery from 'jQuery';
您也可以尝试:
import * as jQuery from 'jQuery';