Search code examples
navbarvuejs3bootstrap-5

Bootstrap 5 navbar not toggling in Vue3


This is a similar question to How can I make navbar items with vue-router-links to toggle the navbar? but I am not allowed to comment there, and the solution given doesn't work me anyway.

I am using Vue 3 and Bootstrap 5 and the following code works exactly as the standard Bootstrap code does i.e. toggling the hamburger menu opens and closes the menu but clicking a link does nothing (other than correctly route to the page being clicked, meaning user has to then tap hamburger menu again to close)

NavBar component:

    <template>
  <nav class="navbar navbar-dark bg-dark navbar-expand-sm">
    <div class="container-fluid">
        <router-link class="navbar-brand" to="/">Birch Farm</router-link> |
        <button class="navbar-toggler" type="button"  
          :class="visible ? null : 'collapsed'" 
          data-bs-toggle="collapse" 
          data-bs-target="#navContent" 
          aria-controls="navContent" 
          :aria-expanded="visible ? 'true' : 'false'"
          @click="visible = !visible" 
          aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navContent">
            <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
              <li class="nav-item active">
                <router-link class="nav-link px-3" active-link="active" to="/" @click="visible = !visible">Home</router-link>
              </li>
              <li class="nav-item">
                <router-link class="nav-link px-3" to="/camping" @click="visible = !visible">Camping &amp; Caravanning</router-link>
              </li>       
              <li class="nav-item">
                <router-link class="nav-link px-3" to="/fishing" @click="visible = !visible">Cat Rough Fishery</router-link>
              </li>  
              <li class="nav-item">
                <router-link class="nav-link px-3" to="/contact" @click="visible = !visible">Contact Us</router-link>
              </li>  
            </ul>
        </div>
    </div>
  </nav>
</template>

<script setup>
  import {ref} from 'vue'
  const visible = ref(false);
</script>
<script>
export default {
  name: "NavBar",
  created() {},
  data() {},
  props: {},
  methods: {},
  components: {}
};
</script>

<style lang="scss" scoped></style>

Not having any of the 'visible' stuff works exactly the same way - this was added when trying the solution given in the above link.


Solution

  • "toggling the hamburger menu opens and closes the menu but clicking a link does nothing (other than correctly route to the page being clicked"

    That's how the Bootstrap Navbar works. It doesn't collapse/hide automatically after clicking a link. Normally you'd have to do something like this to close the Navbar after clicking a link.

    But when using Vue you would toggle the collapse class as needed on the navbar-collapse div using your visible value...

    <div class="navbar-collapse" :class="!visible?'collapse':''" id="navContent">

    Demo: https://codeply.com/p/lHTzN4amfe