dtbhp60824 2018-04-27 08:18
浏览 44
已采纳

Bootstrap 4 JavaScript无法运行Symfony4

I'm trying to use Bootstrap 4 in Symfony 4 using yarn package manager but somehow the JavaScript is not working. I have no errors in the console but when I try to trigger the navbar collapsed button I won't open the navbar.

This is my code:

app.js

var $ = require('jquery');

require("bootstrap/js/dist/");

base.html.twig

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}Welcome!{% endblock %}</title>
        <link rel="stylesheet" href="{{ asset('build/css/app.css') }}">
    </head>
    <body>
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            <a class="navbar-brand" href="#">CRM Fabriek</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Link</a>
                    </li>


                </ul>
                <form class="form-inline my-2 my-lg-0">
                    <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
                    <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
                </form>
            </div>
        </nav>
        {% block body %}{% endblock %}
        <script src="{{ asset('build/js/app.js') }}"></script>
    </body>
</html>

I compiled the js to the build/js/app.js file by using yarn run encore dev

  • 写回答

1条回答 默认 最新

  • doudou521125 2018-04-27 11:26
    关注

    Import Bootstrap’s JavaScript by adding this line to your app’s entry point (usually index.js or app.js):

    import 'bootstrap';
    

    or indicate the path completely

    import 'bootstrap/dist/js/bootstrap';
    

    or if you prefer require()

    require('bootstrap/dist/js/bootstrap');
    

    Alternatively, you may import plugins individually as needed:

    import 'bootstrap/js/dist/util';
    import 'bootstrap/js/dist/dropdown';
    ...
    

    https://getbootstrap.com/docs/4.1/getting-started/webpack/#importing-javascript

    In the webpack-encore documentation says that you must in your webpack.config.js file add a call .autoProvidejQuery() because Bootstrap expects jQuery to be available as a global variable.

    // webpack.config.js
    Encore
    // ...
    .autoProvidejQuery();
    

    http://symfony.com/doc/current/frontend/encore/bootstrap.html#importing-bootstrap-javascript

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助