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 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路