简体   繁体   English

使用Webpack 4时如何访问条目文件中的jQuery对象($)?

[英]How do you access the jQuery object ($) in an entry file when using Webpack 4?

I have a very simple Webpack 4 example where I am trying to access the jQuery object in my entry file in order to manipulate the DOM. 我有一个非常简单的Webpack 4示例,在该示例中,我试图访问条目文件中的jQuery对象以操作DOM。 I have pared it down to what I think is the bare minimum having researched extensively. 我已经将其缩减为我认为已广泛研究的最低要求。

I expect the example to set the background color of the body to cyan upon page load. 我希望该示例在页面加载时将body的背景色设置为青色。 Can't seem to get it to do anything. 似乎无法让它做任何事情。 Looks like the $ variable exposed in the entry file is not the same $ variable in the browser. 貌似$在入口文件暴露的变量是一样的$在浏览器中的变量。

UPDATE - PROBLEM SOLVED : I was misusing jQuery below. 更新-解决了问题 :我在下面滥用jQuery

Original (defective) entry file ( src/app.js ): 原始 (有缺陷的)入口文件( src/app.js ):

import 'jquery';

$(() => {
  $('body').bgColor = 'cyan';
});

Corrected entry file: 更正的条目文件:

import 'jquery';

$(() => {
  $('body').css('background-color', 'cyan');
});

I use the ProvidePlugin as documented here . 我使用此处记录的ProvidePlugin Here is my simple webpack.config.js file: 这是我简单的webpack.config.js文件:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require("webpack");

module.exports = {
  entry: {
    app: './src/app.js',
  },
    output: {
      filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    plugins: [
      new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery'
      }),
      new HtmlWebpackPlugin()
    ],
    devtool: 'inline-source-map'
}

Webpack compiles just fine and the page opens (using webpack-dev-server ), but the background color does not change to cyan as expected. Webpack编译正常,并且页面打开(使用webpack-dev-server ),但是背景颜色没有按预期的那样变为青色。 When I log the value of $('body') in the app.js file, I get undefined . 当我在app.js文件中记录$('body')的值时,得到undefined

Here is the package.json file: 这是package.json文件:

{
  "name": "foo",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start": "webpack-dev-server --inline --open"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "html-webpack-plugin": "^3.2.0",
    "webpack": "^4.16.5",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.5"
  },
  "dependencies": {
    "jquery": "^3.3.1"
  }
}

Feels like I have a fundamental lack of understanding of how this is supposed to work. 像我这样的感觉根本上不了解应该如何工作。 Thanks. 谢谢。 Your help is greatly appreciated. 非常感谢您的帮助。

Apologies. 道歉。 My mistake on misuse of jQuery. 我误用jQuery的错误。 Thanks to @charlietfl for pointing out my mistake in the comments. 感谢@charlietfl指出我在评论中的错误。 I used. 我用了。 $('body').bgColor = "cyan" instead of $('body').css('background-color', 'cyan') . $('body').bgColor = "cyan"而非$('body').css('background-color', 'cyan') Everything else is properly configured. 其他所有内容均已正确配置。 So if you need a simple example, here it is. 因此,如果您需要一个简单的示例,就在这里。

I will edit the question and point out my mistake. 我将编辑问题并指出我的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM