【问题标题】:webpack - ReferenceError: $ is not definedwebpack - ReferenceError: $ 未定义
【发布时间】:2021-07-30 15:14:26
【问题描述】:

我已经阅读了很多关于这个问题的答案,但我无法解决我的问题,所以我来这里寻求帮助......

这是我的 webpack 配置文件:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
// https://github.com/webpack-contrib/mini-css-extract-plugin
const webpack = require('webpack');
const jquery = require('jquery');

module.exports = {
    mode: 'development',
    entry: './assets/js/app.js',
    output: {
        path: path.resolve(__dirname, 'public/dist'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                        options: {
                            publicPath: '../',
                            hmr: process.env.NODE_ENV === 'development',
                        },
                    },
                    'css-loader',
                ],
            },
            {
                test: /\.(png|jpg|gif)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 100000,
                        },
                    },
                    {
                        loader:"file-loader",
                        options:{
                            name:'[name].[ext]',
                            outputPath:'images/' //the images will be emited to dist/images/ folder
                        }
                    }
                ],
            },
            {
                // Exposes jQuery for use outside Webpack build
                test: require.resolve('jquery'),
                use: [{
                    loader: 'expose-loader',
                    options: 'jQuery'
                }, {
                    loader: 'expose-loader',
                    options: '$'
                }]
            }
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: '[name].css',
            chunkFilename: '[name]-[id].css',
            ignoreOrder: false,
        }),
        /* Use the ProvidePlugin constructor to inject jquery implicit globals */
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery'",
            "window.$": "jquery"
        })
    ],
    /*resolve: {
        alias: {
            jquery: "jquery/src/jquery"
        }
    }*/
};

还有我的入口文件 app.js :

// Import CSS
import 'bootstrap/dist/css/bootstrap.css';
import '@fortawesome/fontawesome-free/js/all';
import '../css/style.css';

// Import JS
import $ from 'jquery';
window.$ = jQuery;
window.jQuery = jQuery;

import * as JSZip from 'jszip'; //export xlsx

import 'bootstrap';

//require('webpack-jquery-ui');
//require('webpack-jquery-ui/css');

我继续尝试不同的解决方案,但当我在代码中使用 jQuery 时,我仍然得到:

ReferenceError: $ 未定义

你能帮帮我吗?

【问题讨论】:

  • 为什么要将jQuery 命名或重命名为“jquery”?
  • 我在哪里做的??
  • 我认为我在使用 jQuery,这是一个错误吗?我是否应该只在我在 webpack conf 中作为条目声明的 js 文件中使用它?

标签: javascript webpack


【解决方案1】:

在你的代码中看看这个:

import $ from 'jquery';
window.$ = jQuery;
window.jQuery = jQuery;

想一想。

.

.

.

.

.

.

.

.

.

.

.

.

请注意,您从未定义过jQuery。修复它。

import $ from 'jquery';
window.$ = $;
window.jQuery = $;

【讨论】:

    【解决方案2】:

    将 jquery 库添加为外部的一种解决方案。第二种解决方案是将 jquery 添加到包中。

    1.外部 webpack.js

    externals: {   
      jquery: 'jQuery'
    }
    plugins: [   new webpack.ProvidePlugin({      
      $: 'jquery',
      jQuery: 'jquery',
      'window.jQuery': 'jquery'    
    })]
    

    将 jquery 添加到 html

    <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous">
    

    在js中添加jquery

    import $ from 'jquery';
    
    const test = $('h1').text();
    console.log(`from jquery: $ {test}`);
    

    2。第二种方案,将jquery添加到bundle中首先需要在本地安装jquery。

    yarn add -D jquery
    

    在js中添加jquery

    import $ from 'jquery';
    
    const test = $('h1').text();
    console.log(`from jquery: $ {test}`);
    

    这可以使用 splitChunks 进行优化和拆分,但这是另一个条目的故事;)

    我添加了一个外部jquery使用的例子

    【讨论】:

    • 所以我必须在视图中添加 jQuery 才能在那里使用它?
    • 是的,如果您想使用外部 jquery 库,请参阅我的第一个示例。最好看看我链接的例子。
    【解决方案3】:

    好吧,这个错误很愚蠢,在布局结束时调用了 app.js,我试图在视图中使用 jQuery。

    因此,请确保检查损坏的代码和包含 jquery 的包含被调用的顺序。

    【讨论】:

    • 这实际上是我的问题...哈哈,谢谢提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2019-03-19
    • 2018-10-29
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多