简体   繁体   中英

Bootstrap 4 JavaScript not working 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM