Laravel添加jquery

文章介绍了如何在Laravel项目中通过命令行安装jQuery并在webpack.mix.js中设置,以便在构建过程中将其暴露给全局,并使用ProvidePlugin确保jQuery可用。
摘要由CSDN通过智能技术生成

命令提示符在项目根目录执行:

npm install jquery --save-dev

项目根目录webpack.mix.js更新内容:

const mix = require("laravel-mix");
const path = require("path");
const webpack = require("webpack");
/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js("resources/js/app.js", "public/js")
    .react()
    .sass("resources/sass/app.scss", "public/css")
    .css("resources/css/app.css", "public/css")
    .sourceMaps()
    // 引入 jQuery 并将其暴露给全局作用域
    .webpackConfig({
        resolve: {
            alias: {
                "@": path.resolve("resources/js"),
                // 将 jQuery 暴露给 window.$ 和 window.jQuery
                jquery: "jquery/src/jquery",
            },
        },
        plugins: [
            new webpack.ProvidePlugin({
                $: "jquery",
                jQuery: "jquery",
                "window.$": "jquery",
                "window.jQuery": "jquery",
            }),
        ],
    });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值