Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJS close Hamburger Menu on Route Change

I have a simple VueJS App with a Navigation Bar from Bootstrap:

<template>
  <header id="header">
    <nav class="navbar mynavbar navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="logo" href="index.html"><img src="images/logo.png" alt=""></a>
        </div>

        <div class="collapse navbar-collapse" id="navbar-collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><router-link to="/home"><a>Home</a></router-link></li>
             <li><router-link to="/about"><a>About Us</a></router-link></li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </div><!-- /.container -->
    </nav>
  </header>
</template>

Now I want to ensure that when I change the route, the Bootstrap Menu gets closed. What is the best way to accomplish this?

like image 570
user5417542 Avatar asked Mar 17 '17 11:03

user5417542


4 Answers

Instead of adding event handlers to every router-link, you can simply watch the $route property for changes:

<script>
export default {
  watch: {
    '$route' () {
      $('#navbar-collapse').collapse('hide');
    }
  }
}
</script>
like image 99
cinder Avatar answered Nov 14 '22 17:11

cinder


With vue3 and bootstrap5, adding this to the router-link element:

data-bs-toggle="collapse" data-bs-target=".navbar-collapse" 

did not work for me: the menu would close without the link being followed. So, I added this to router/index.js:

router.beforeEach(() => {
    document.getElementById('navbarCollapse').classList.remove('show');
})

with navbarCollapse being the id of the div holding the menu/nav items. It seemed to do the trick.

If necessary you can adjust the hamburger button state: its class list would need to contain "collapsed" and aria-expanded set to "false". This gets corrected though when the user clicks the button again, so I didn't opt to write that code.

like image 23
Herbert Van-Vliet Avatar answered Nov 14 '22 19:11

Herbert Van-Vliet


Without Bootstrap and jQuery, can be easily achieved using class toggle and watch route changes.

<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" 
    @click="toggledNav = !toggledNav"
>
   <span aria-hidden="true"></span>
   <span aria-hidden="true"></span>
   <span aria-hidden="true"></span>
</a>

Script:

export default {
  data () {
    return {
      toggledNav: false
    }
  },
  watch: {
    '$route' () {
      this.toggledNav = false
    }
  }
}
like image 19
claudios Avatar answered Nov 14 '22 17:11

claudios


You could give this a try:

<template>
    <header id="header">
        <nav class="navbar mynavbar navbar-fixed-top">
            <div class="container">
                <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="logo" href="index.html"><img src="images/logo.png" alt=""></a>
                </div>

                <div class="collapse navbar-collapse" id="navbar-collapse">
                    <ul class="nav navbar-nav navbar-right">
                        <li><router-link @click.native="closeMenu()" to="/home"><a>Home</a></router-link></li>
                        <li><router-link @click.native="closeMenu()" to="/about"><a>About Us</a></router-link></li>
                    </ul>
                </div><!-- /.navbar-collapse -->
            </div><!-- /.container -->
        </nav>
    </header>
</template>
<script>
    export default {
        methods: {
            closeMenu() {
                $('#navbar-collapse').collapse('hide');
            }
        }
    }
</script>
like image 4
Tienou Avatar answered Nov 14 '22 19:11

Tienou