jQuery 未定义(使用 Webpack) [英] jQuery is not defined (using Webpack)

查看:35
本文介绍了jQuery 未定义(使用 Webpack)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个脚本:

<script type="text/javascript" src="main.min.js"></script>
<script type="text/javascript" src="script-A.js"></script>
<script type="text/javascript" src="script-B.js"></script>

main.min.js 是我用 Webpack 创建的文件.该文件包含我的项目所需的所有脚本,包括我作为 NPM 包安装的 jQuery.

main.min.js is a file I create with Webpack. This file includes all the scripts I need for my project, including jQuery, which I have installed as a NPM package.

script-A.jsscript-B.js 是不幸的我不能包含在我的主 Webpack 文件中的脚本,所以我需要单独加载它们.这些脚本需要 jQuery 作为依赖;但即使 jQuery 包含在 main.min.js 中,我在调用它们时也会收到 jQuery is not defined 错误.

script-A.js and script-B.js are scripts that unfortunately I can't include in my main Webpack file, so I need to load them separately. These scripts need jQuery as a dependency; but even though jQuery is included in main.min.js, I get a jQuery is not defined error when they are invoked.

顺便说一下,在我的 Webpack 文件中,我已经有了这些代码行,这让我可以在通过 Webpack 处理的任何脚本中使用 jQuery,但它似乎对其他脚本没有任何影响:

By the way, in my Webpack file I already have these lines of code, which let me use jQuery in any script I handle through Webpack, but it doesn't seem to have any effect on the other scripts:

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

我该如何修复错误?理想情况下,我需要一个解决方案,我不必接触 script-A.jsscript-B.js 因为它们属于插件.

How can I fix the error? Ideally, I need a solution where I don't have to touch script-A.js and script-B.js since they belong to a plugin.

控制台代码片段

Uncaught ReferenceError: jQuery is not defined
    at script-A.js?ver=2.9.6:1
Uncaught ReferenceError: jQuery is not defined
    at script-B.js:617
    at script-B.js:620

PACKAJGE.JSON

{
  "name": "mysite",
  "version": "1.0.0",
  "description": "mysite",
  "main": "webpack.config.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "jquery": "^3.2.1",
    "jquery-lazy": "^1.7.5",
    "salvattore": "^1.0.9",
    "webpack": "^3.6.0"
  },
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-es2015": "^6.24.1",
    "browser-sync": "^2.18.13",
    "browser-sync-webpack-plugin": "^1.2.0",
    "css-loader": "^0.28.7",
    "extract-text-webpack-plugin": "^3.0.1",
    "file-loader": "^1.1.5",
    "node-sass": "^4.5.3",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.19.0",
    "url-loader": "^0.6.2",
    "webpack-merge": "^4.1.0"
  }
}

推荐答案

你需要利用 expose-loader 以使 jQuery 可用于全局范围内的其他脚本.

You need to make use of the expose-loader in order to make jQuery available to the other scripts on the global scope.

module: {
  rules: [{
    test: require.resolve('jquery'),
    use: [{
        loader: 'expose-loader',
        options: 'jQuery'
    },{
        loader: 'expose-loader',
        options: '$'
    }]
  }]
}

这篇关于jQuery 未定义(使用 Webpack)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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