3

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
  • Did you find a solution ? My collapse divs also dont work Commented Jan 23, 2019 at 20:25

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.