简体   繁体   English

无法使用 webpack 和 Laravel 混合加载 popper.js

[英]Can't load popper.js with webpack and Laravel mix

I'm using bootstrap 4 beta and Laravel 5.4 on my project and loading my js dependencies with npm and laravel mix.我在我的项目上使用 bootstrap 4 beta 和 Laravel 5.4,并使用 npm 和 laravel 混合加载我的 js 依赖项。 So far everything has been working great, except when i'm trying to use the booostrap js methods.到目前为止,一切都运行良好,除非我尝试使用 booostrap js 方法。 It throws me the error "Bootstrap dropdown require Popper.js", so i downloaded and loaded it in the bootstrap.js and webpack.mix.js files, but it still is asking for this dependency, can you tell me what i did wrong?它抛出错误“Bootstrap dropdown require Popper.js”,所以我下载了它并将其加载到 bootstrap.js 和 webpack.mix.js 文件中,但它仍然要求这种依赖,你能告诉我我做错了什么吗?

boostrap.js助推器.js

try {
  window.$ = window.jQuery = require('jquery');

  require('popper.js');
  require('datatables.net');
  require('datatables.net-autofill');
  require('datatables.net-buttons');
  require('bootstrap');
} catch (e) {
  console.log(e);
}

webpack.mix.js webpack.mix.js

const { mix } = require('laravel-mix');

mix.js([
  'resources/assets/js/app.js',
  'node_modules/jquery/dist/jquery.min.js',
  'node_modules/popper.js/dist/umd/popper.js',
  'node_modules/bootstrap/dist/js/bootstrap.js',
  'node_modules/datatables.net/js/jquery.dataTables.js',
  'node_modules/datatables.net-buttons/js/dataTables.buttons.js',
  'node_modules/datatables.net-autofill/js/dataTables.autoFill.js',
], 'public/js')
  .sass('resources/assets/sass/app.scss', 'public/css');

I was also facing this issue and I fixed this by: 我也面临这个问题,我通过以下方式解决了这个问题:

You've to install the popper.js : 你要安装popper.js:

npm install popper.js --save

And loading it before bootstrap file in resources/asset/js/bootstrap.js 在resources / asset / js / bootstrap.js中的bootstrap文件之前加载它

    try {
        window.$ = window.jQuery = require('jquery');
        window.Popper = require('popper.js/dist/umd/popper.js').default;
        require('bootstrap');
    } catch (e) {
    }

And in the webpack.mix.js: 在webpack.mix.js中:

   mix.autoload({
    'jquery': ['$', 'window.jQuery', "jQuery", "window.$", "jquery", "window.jquery"],
    'popper.js/dist/umd/popper.js': ['Popper', 'window.Popper']
});

There are several ways of solving it. 有几种方法可以解决它。 You can go here for more details: 你可以到这里了解更多细节:

https://github.com/FezVrasta/popper.js/issues/287 https://github.com/FezVrasta/popper.js/issues/287

I have same issue but it's done right now 我有同样的问题,但现在已经完成了

I just edit this line in resources/asset/js/bootstrap.js 我只是在resources / asset / js / bootstrap.js中编辑这一行

 window._ = require('lodash'); // new line #1 import Popper from 'popper.js/dist/umd/popper.js'; /** * 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'); // new line #2 and #3 window.Popper = Popper; require('bootstrap'); } catch (e) {} 

You can also read to the full guide here 您还可以阅读到完整指南在这里

Hopefully it'll help your issue too :) 希望它也能帮助你解决问题:)

After trying both answers for this issue, I was still receiving the same error. 在尝试这个问题的两个答案之后,我仍然收到同样的错误。 What resolved it for me was the following 为我解决的是以下内容

Works for Laravel 5.8 适用于Laravel 5.8

bootstrap.js bootstrap.js

window._ = require('lodash');
import Popper from 'popper.js/dist/umd/popper.js'; ///<------ add this line

try {
    window.Popper = Popper; //<------ add this line
    window.$ = window.jQuery = require('jquery');

    require('bootstrap');
} catch (e) {}

webpack.mix.js webpack.mix.js

mix.js('resources/js/app.js', 'public/js').sourceMaps(); //<------ add this line and remove any other lines for mix.js

Yet another solution for laravel 9.x and laravel-mix 6.x laravel 9.x 和 laravel-mix 6.x 的另一种解决方案

npm install --save-dev bootstrap@^4

app.js应用程序.js

window.Popper = require('popper.js');
window.bootstrap = require('bootstrap');

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

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