如何使用 laravel-mix 添加 Bootstrap、jQuery 和 popper.js 到 Laravel 8 项目?

How to add Bootstrap, jQuery, and popper.js to Laravel 8 project using laravel-mix?

据我了解,以下命令应安装 Bootstrap、jQuery 和 popper.js:

composer require laravel/ui
php artisan ui bootstrap
npm install && npm run dev

在 运行 这些命令之后,我现在可以使用 Bootstrap class 名称,但我不能使用 jQuery。当我尝试添加一些 jQuery 代码时,出现以下错误:Uncaught ReferenceError: $ is not defined

我在文档的开头添加了 <script src="{{ asset('js/app.js') }}"></script><link href="{{ asset('css/app.css') }}" rel="stylesheet">

我是不是遗漏了什么?

示例代码:

<!doctype html>
<html class="no-js" lang="da">

<head>
    <script src="{{ asset('js/app.js') }}"></script>
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
<script>
    $(document).ready(function () {
        alert(0);
    });
</script>
</body>
</html>

确保您的 <script> 标签中没有 defer 导入 app.js

这通常会导致 Laravel Mix 出现 $ is not defined 错误。

我在使用 laravel/ui 安装和使用软件包时遇到了很多问题。它安装的 bootstrap 版本也已过时,因为它只有 4.6 版,最新版本是,截至今天:5.1.1 (https://github.com/twbs/bootstrap/releases),所以我决定尝试自己安装软件包, 最后一切正常。

我所做的是使用以下命令安装所有软件包:

#We do this because it will take care of a lot of things for us
composer require laravel/ui
php artisan ui bootstrap

#And now we upgrade bootstrap and add new popper.js version
npm install bootstrap@latest @popperjs/core --save-dev

这应该安装所有 node_modules,包括 bootstrap v. 5.1.1、jquery v. 3.6、@popperjs/core v. 2.10.2 和 package.json 现在看起来像这样:

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "mix",
        "watch": "mix watch",
        "watch-poll": "mix watch -- --watch-options-poll=1000",
        "hot": "mix watch --hot",
        "prod": "npm run production",
        "production": "mix --production"
    },
    "devDependencies": {
        "@popperjs/core": "^2.10.2",
        "axios": "^0.21",
        "bootstrap": "^5.1.1",
        "jquery": "^3.6",
        "laravel-mix": "^6.0.6",
        "lodash": "^4.17.19",
        "popper.js": "^1.16.1",
        "postcss": "^8.1.14",
        "sass": "^1.32.11",
        "sass-loader": "^11.0.1"
    }
}

现在在 resources/js/bootstrap.js 中,我更改了代码,现在看起来像这样:

window._ = require('lodash');

/**
 * We'll load jQuery and the Bootstrap jQuery plugin which provides support
 * for JavaScript based Bootstrap features such as modals and tabs. This
 * code may be modified to fit the specific needs of your application.
 */

try {
    window.$ = window.jQuery = require('jquery');
    window.Popper = require('@popperjs/core');
    window.bootstrap = require('bootstrap');

} catch (e) {}

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

这次改动后,就运行

npm run dev

现在一切正常。这是一个示例 HTML/JS 页面,将它们全部用于测试

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>jQuery, popper.js, and Bootstrap</title>
    
    {{--    Load compiled CSS    --}}
    <link rel="stylesheet" href={{ asset('css/app.css') }}>
    
    {{--  popper.js CSS example  --}}
    <style>
        #tooltip {
            background: #333;
            color: white;
            font-weight: bold;
            padding: 4px 8px;
            font-size: 13px;
            border-radius: 4px;
        }
    </style>
</head>
<body>

{{--  Test Bootstrap css  --}}
<div class="alert alert-success mt-5" role="alert">
    Boostrap 5 is working using laravel 8 mix!
</div>

{{--  popper.js HTML example  --}}
<button id="button" aria-describedby="tooltip">My button</button>
<div id="tooltip" role="tooltip">My tooltip</div>

{{--    Load compiled Javascript    --}}
<script src="{{ asset('js/app.js') }}"></script>
<script>
    //Test jQuery
    $(document).ready(function () {
        console.log('jQuery works!');

        //Test bootstrap Javascript
        console.log(bootstrap.Tooltip.VERSION);
    });

    //Test popper.js
    const button = document.querySelector('#button');
    const tooltip = document.querySelector('#tooltip');
    const popperInstance = Popper.createPopper(button, tooltip);
</script>

</body>
</html>