【问题标题】:dropdown menu is not working with bootstrap 4 / webpack encore / symfony 4 helppp下拉菜单不适用于 bootstrap 4 / webpack encore / symfony 4 helppp
【发布时间】:2018-12-10 17:32:56
【问题描述】:

我想使用引导下拉菜单,但是当我点击它时它什么也没做。 我将它与 webpack encore 一起使用。

这里是html:

    {% block stylesheets %}
        <link rel="stylesheet" href="{{ asset('build/css/app.css') }}">
    {% endblock %}

...

        <ul class="navbar-nav ml-auto">
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="dropdown09"
                   data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dropdown</a>
                <div class="dropdown-menu" aria-labelledby="dropdown09">
                    <a class="dropdown-item" href="#">Action</a>
                    <a class="dropdown-item" href="#">Another action</a>
                    <a class="dropdown-item" href="#">Something else here</a>
                </div>
            </li>
        </ul>

(如果我使用下面的 3 个链接,它可以工作,但我想让它与 app.js 一起工作)

{% block javascripts %}
    {#<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>#}
    {#<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>#}
    {#<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>#}

    <script src="{{ asset('build/app.js') }}"></script>
{% endblock %}

这是我的文件 webpack.config.js :

var Encore = require('@symfony/webpack-encore');
var Dotenv = require('dotenv-webpack');

Encore
// directory where compiled assets will be stored
    .setOutputPath('public/build/')
    // public path used by the web server to access the output path
    .setPublicPath('/build')
    // only needed for CDN's or sub-directory deploy
    //.setManifestKeyPrefix('build/')

    /*
     * ENTRY CONFIG
     *
     * Add 1 entry for each "page" of your app
     * (including one that's included on every page - e.g. "app")
     *
     * Each entry will result in one JavaScript file (e.g. app.js)
     * and one CSS file (e.g. app.css) if you JavaScript imports CSS.
     */

    // will create public/build/app.js
    .addEntry('app', './assets/js/app.js')

    // will create and public/build/css/app.css
    .addStyleEntry('css/app', ['./assets/css/app.scss'])

    //.addEntry('page1', './assets/js/page1.js')
    //.addEntry('page2', './assets/js/page2.js')

    // enable vue.js
    .enableVueLoader()

    // empty the outputPath dir before each build
    .cleanupOutputBeforeBuild()

    // show OS notifications when builds finish/fail
    .enableBuildNotifications()

    // will require an extra script tag for runtime.js
    // but, you probably want this, unless you're building a single-page app
    .enableSingleRuntimeChunk()

    /*
     * FEATURE CONFIG
     *
     * Enable & configure other features below. For a full
     * list of features, see:
     * https://symfony.com/doc/current/frontend.html#adding-more-features
     */
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
    .enableSourceMaps(!Encore.isProduction())
    // enables hashed filenames (e.g. app.abc123.css)
    .enableVersioning(Encore.isProduction())

    // enables Sass/SCSS support
    .enableSassLoader()

    // uncomment if you use TypeScript
    //.enableTypeScriptLoader()

    // allow legacy applications to use $/jQuery as a global variable
    .autoProvidejQuery()

    .addPlugin(new Dotenv({
        path: './.env',
        safe: false
    }))

    .autoProvideVariables({
        $: 'jquery',
        jQuery: 'jquery'
    });
;


module.exports = Encore.getWebpackConfig();

这里是 app.js:

var $ = require('jquery');
require('popper.js');
require('bootstrap');

app.scss:

@import "~bootstrap/scss/bootstrap";
//@import "bootstrap.min.css";
@import "navbar.css";

我跑了:

yarn install
yarn add bootstrap-sass --dev
yarn add bootstrap --dev
yarn add jquery popper.js --dev
yarn run encore dev

请帮忙 请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙请请帮忙请帮忙请帮忙请帮忙请帮忙请帮忙

【问题讨论】:

  • 我想你可能需要说请多帮助几次。从你已经说过的 30 次中不清楚。
  • 您在控制台中看到错误吗?
  • 控制台没有错误

标签: javascript php twitter-bootstrap symfony webpack


【解决方案1】:

我找到了!我们必须在 webpack.config.js 中注释这部分:

// will require an extra script tag for runtime.js
// but, you probably want this, unless you're building a single-page app
// .enableSingleRuntimeChunk()

【讨论】:

  • 虽然我感谢你的问题,但我不需要注释掉 .enableSingleRuntimeChunk() 就可以让 Symfony4 + webpack + Bootstrap 4 很好地协同工作。但是,我确实知道无法将 KnpMenuBundle 包含在组合中。我已经用老式的方式重写了我所有的菜单 - 在 html 中(href 使用 twig 的 path(...) 除外)。
猜你喜欢
  • 1970-01-01
  • 2021-06-20
  • 2021-08-16
  • 2020-03-08
  • 2014-11-25
  • 2018-08-09
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
相关资源
最近更新 更多