0

I'm trying to get my collapsable navbar working with Vue + Bootstrap in Laravel 7 but am having no luck. The navbar shows without issues but when I resize and attempt to collapse it, the button does nothing and throws no errors.

I have imported bootstrap-vue into my app.ts file and it should be enabled but it does not seem to be.

app.ts

import "./bootstrap";
import Vue from "vue";
import BootstrapVue from "bootstrap-vue"; //Importing

Vue.use(BootstrapVue); // Telling Vue to use this in whole application

// Require everything withing the /components directory as a Vue component
const files = require.context("./", true, /\.vue$/i);
files.keys().map(key =>
    Vue.component(
        key
            .split("/")
            .pop()
            ?.split(".")[0] ?? "",
        files(key).default
    )
);

new Vue({
    el: "#app"
});

navbar file (main.blade.php)

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<head>
    <meta charset="utf-8">
    <meta name="csrf-token"
          content="{{ csrf_token() }}">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>nCoV Stats</title>
    <link href="{{ asset('css/app.css') }}"
          rel="stylesheet">
</head>

<body class="d-flex flex-column min-vh-100">
    <!-- Navbar -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <a class="navbar-brand"
           href="#">Navbar</a>
        <button class="navbar-toggler"
                type="button"
                data-toggle="collapse"
                data-target="#navbarNav"
                aria-controls="navbarNav"
                aria-expanded="false"
                aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse"
             id="navbarNav">
            <ul class="navbar-nav">
                <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="#">Features</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link"
                       href="#">Pricing</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link disabled"
                       href="#"
                       tabindex="-1"
                       aria-disabled="true">Disabled</a>
                </li>
            </ul>
        </div>
    </nav>

    <!-- Content -->
    <main id="app"
          class="wrapper flex-grow-1">
        @yield('content')
    </main>

    <!-- Footer -->
    <footer>
        <div class="container">
            <div class="row">
                <div class="col-8">
                    A Joe Scotto product.
                </div>
                <div class="col-4"></div>
            </div>
        </div>
    </footer>

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

</html>

From my understanding, this should just work since I'm importing it inside of app.ts

Joe Scotto
  • 10,936
  • 14
  • 66
  • 136
  • This `new Vue({ el: "#app" });` means that the vue instance will work only on the tag with `id=app`, wich is `
    ` tag, and your nav is outside that tag
    – porloscerros Ψ May 01 '20 at 04:19
  • @porloscerrosΨ That is what I thought as well but moving it within there still does not work. – Joe Scotto May 02 '20 at 05:01

1 Answers1

3

I think it is because you're not using any Bootstrap-Vue components in your markup. In your case the navbar would have to look something like:

<b-navbar toggleable="lg" type="light" variant="light">
    <b-navbar-brand href="#">NavBar</b-navbar-brand>

    <b-navbar-toggle target="nav-collapse"></b-navbar-toggle>

    <b-collapse id="nav-collapse" is-nav>
      <b-navbar-nav>
        <b-nav-item href="#">Home</b-nav-item>
        <b-nav-item href="#">Features</b-nav-item>
        <b-nav-item href="#">Pricing</b-nav-item>
      </b-navbar-nav>
    </b-collapse>
  </b-navbar>

You can find more info about the navbar component here

Additionally, like porloscerros Ψ mentioned, you need to set id=app on either the body element or create a div with id=app which contains your (Bootstrap-Vue) components.

Sidney Gijzen
  • 1,931
  • 3
  • 24
  • 27