【发布时间】:2016-09-23 22:34:06
【问题描述】:
我最近从使用 Require.js 切换到使用带有 Babel 的 WebPack。过去我会在我的 JS 模块中使用 CommonJS 标准,就像这样
var $ = require('jquery');
require('bootstrap');
由于 Bootstrap 是一个 jQuery 插件,jQuery 会首先加载,而 bootstrap 会在第二个加载。
Babel 允许我使用 ES6 import 语句。但是当我写的时候
import $ from 'jquery';
import Bootstrap from 'bootstrap';
我收到$ is undefined 的错误。 Bootstrap 假设 window.$ 存在,但 import 不会污染窗口对象,这是一件好事,但我的代码是这样的:
// legacy loading for bootstrap
var $ = require('jquery');
window.$ = $;
require('bootstrap');
// the rest of the imports
import _ from 'underscore';
必须有更好的解决方案。任何帮助表示赞赏。
【问题讨论】:
-
你试过了吗:从'jquery'导入jQuery;常量 $ = jQuery ;
-
这就是我在后期添加 bootstrap 4 的方式gist.github.com/vishim/9c3e1b06a8f513c923e5187490d8f917
-
如果你使用的是browserify,也许使用npmjs.com/package/browserify-shim ?
标签: twitter-bootstrap import ecmascript-6