如何为Bootstrap4 Popups和Tooltips(Popper.js)配置webpack

时间:2017-08-23 13:10:20

标签: javascript jquery twitter-bootstrap

我尝试从bootstrap 4 alpha更新到beta版,我正在使用webpack!
到目前为止,我已成功迁移我的代码以兼容,唯一的问题是我无法使用工具提示和下拉菜单工作!根据文档,他们说这些功能取决于popper.js,并给出了一个如何配置我工作的例子!
我已按照这些说明进行操作,但现在当我想使用下拉列表和工具提示时,我得到TypeError: popper is undefined

webpack.mix.js

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

mix.webpackConfig({
    output: {
        path: path.join(__dirname, "/public"),
        publicPath: '/',
        chunkFilename: 'js/modules/[name].js'
    },
    plugins:[
        new webpack.ProvidePlugin({
            Moment: 'moment',
            'window.Moment': 'moment',
            Popper: ['popper.js', 'default'],
            popper: ['popper.js', 'default'],
            // 'window.Po': ['popper.js', 'default']
        }),
        new webpack.optimize.MinChunkSizePlugin({minChunkSize: 100000})
    ]
});

/*
 |--------------------------------------------------------------------------
 | 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.autoload({
    vue: [ 'Vue', 'window.Vue' ],
    // moment: ['window.moment', 'Moment'],
    jquery: ['$','jQuery','window.jQuery'],
    tether: ['window.Tether', 'Tether'],
    axios: ['axios','window.axios'],
    'cookies-js': ['Cookies']
});

mix.js('resources/assets/js/app.js', 'public/js')
    .extract(['vue','lodash','cookies-js','jquery','moment','tether','bootstrap','axios'])
    .sass('resources/assets/sass/app.scss', 'public/css')
    .js('resources/assets/js/admin.js', 'public/js')
    .sass('resources/assets/sass/admin.scss', 'public/css')
    .version()
    .browserSync({
        proxy: 'localhost:8000'
    });

我无法弄清楚我在这里失踪了什么

更新
HTML代码

<li class="nav-drop dropdown">
  <a href="#currency-menu" data-toggle="dropdown">{{ moneyCurrencyCode() }}</a>
  <ul id="currency-menu" class="list nav-drop-menu dropdown-menu">
      <li><a data-toggle="currency" data-currency="MZN" href="#">MZN</a></li>
      <li><a data-toggle="currency" data-currency="ZAR" href="#">ZAR</a></li>
      <li><a data-toggle="currency" data-currency="USD" href="#">USD</a></li>
      <li><a data-toggle="currency" data-currency="EUR" href="#">EUR</a></li>
  </ul>
</li>

3 个答案:

答案 0 :(得分:1)

这实际上对我有用:

let mix = require('laravel-mix');
let webpack = require('webpack');

mix.js('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css')
   .webpackConfig({
     plugins: [
       new webpack.ProvidePlugin({
         $: 'jquery',
         jQuery: 'jquery',
         'window.jQuery': 'jquery',
         Popper: ['popper.js', 'default'],
       })
     ]
   })
   .version();

答案 1 :(得分:1)

为了使用bootsrap4(beta)弹出窗口,工具提示,使用laravel webpack的下拉菜单,您可以下载popper.js以及bootstrap 4。

npm install bootstrap:4.0.0-beta popper.js --save

之后,您可以编辑resources / assets / js / bootstrap.js文件以包含popper.js:

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

    require('bootstrap/js/src/dropdown');
    require('bootstrap/js/src/popover');
    require('bootstrap/js/src/tooltip');

} catch (e) {
}

最后,您还可以编辑webpack.mix.js文件:

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

答案 2 :(得分:0)

经过一些调查,我发现问题不在于配置,而在于HTML代码!由于我从bootstrap 4 alpha迁移到beta,我不得不更改href =&#34; #currency-menu&#34; to href =&#34;#&#34;在下拉菜单上,所以看起来有以下

HTML代码

<li class="nav-drop dropdown">
  <a href="#" data-toggle="dropdown">{{ moneyCurrencyCode() }}</a>
  <ul id="currency-menu" class="list nav-drop-menu dropdown-menu">
      <li><a data-toggle="currency" data-currency="MZN" href="#">MZN</a></li>
      <li><a data-toggle="currency" data-currency="ZAR" href="#">ZAR</a></li>
      <li><a data-toggle="currency" data-currency="USD" href="#">USD</a></li>
      <li><a data-toggle="currency" data-currency="EUR" href="#">EUR</a></li>
  </ul>
</li>