webpack jquery不起作用,'$未定义'

时间:2018-01-02 13:05:12

标签: jquery webpack yarnpkg

我有一个单独的.js用于所有脚本等。在顶部我导入我使用webpack / yarn添加的所有必需脚本。

CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<LoadProfile> cq = cb.createQuery(LoadProfile.class);
Root<LoadProfile> loadProfileRoot = cq.from(LoadProfile.class);
Join<LoadProfile, ForwardPower> join = loadProfileRoot.join(LoadProfile_.forwardPower);
List<Predicate> conditions = new ArrayList();
conditions.add(cb.equal(loadProfileRoot.get(LoadProfile_.pk).get(Pk_.dataId), join.get(
    ForwardPower_.pk).get(Pk_.dataId)));
conditions.add(cb.equal(loadProfileRoot.get(LoadProfile_.pk).get(Pk_.occurrenceTime),
    join.get(ForwardPower_.pk).get(Pk_.occurrenceTime)));

conditions.add(
    cb.greaterThanOrEqualTo(loadProfileRoot.get(LoadProfile_.pk).get(Pk_.occurrenceTime),
        config.getDataStartTime()));
conditions.add(
    cb.lessThanOrEqualTo(loadProfileRoot.get(LoadProfile_.pk).get(Pk_.occurrenceTime),
        config.getDataEndTime()));

cq.select(loadProfileRoot);
cq.where(conditions.toArray(new Predicate[]{}));
Query query = session.createQuery(cq);
List list = query.setFirstResult(0).setMaxResults(1000).getResultList();

但是由于我改为使用webpack导入jQuery而不是链接到实际的jQuery脚本,我在Firefox控制台中出现错误。

import 'jquery';
import 'bootstrap';

3 个答案:

答案 0 :(得分:0)

使用jQuery,您需要将名称$设置为从库中专门导入,而不是仅导入整个库并依赖副作用。尝试:

import $ from "jquery";

答案 1 :(得分:0)

在配置文件中,您可以使用此本机插件:

  new webpack.ProvidePlugin({
     $: 'jquery',
    '$': 'jquery',
    jquery: 'jquery',
    jQuery: 'jquery',
  }),

答案 2 :(得分:0)

您可以尝试在main.jsapp.js中使用此方法,无论您使用的是全局Javascript文件。

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