VueJS关闭路线改变的汉堡菜单

时间:2017-03-17 11:11:05

标签: javascript twitter-bootstrap twitter-bootstrap-3 vue.js vuejs2

我有一个简单的VueJS应用程序和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>

现在我想确保在更改路径时,Bootstrap菜单会关闭。实现这一目标的最佳方法是什么?

6 个答案:

答案 0 :(得分:12)

您可以只关注router-link属性进行更改,而不是向每个$route添加事件处理程序:

<script>
export default {
  watch: {
    '$route' () {
      $('#navbar-collapse').collapse('hide');
    }
  }
}
</script>

答案 1 :(得分:3)

你可以尝试一下:

<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>

答案 2 :(得分:0)

没有Bootstrap和jQuery,可以使用类切换和观看路线更改轻松实现。

<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>

脚本:

export default {
  data () {
    return {
      toggledNav: false
    }
  },
  watch: {
    '$route' () {
      this.toggledNav = false
    }
  }
}

答案 3 :(得分:0)

如果您使用的是bootstrap-vue和vue-router。

<script>
    export default {
        watch: {
          '$route' () {
                const element = document.querySelector("#nav-collapse");
                let isShown = element.classList.contains("show");
                if(isShown){
                    this.$root.$emit('bv::toggle::collapse', 'nav-collapse')
                }
            }
        }
    }
</script>
<template>
  <b-navbar toggleable="lg" type="" class="p-0">
      <b-navbar-toggle target="nav-collapse" id="navbar-collapse">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
      </b-navbar-toggle>
      <b-collapse id="nav-collapse" is-nav>
          <div class="nav-wrapper w-100">
             <ul class="app-navigation__list">
                <router-link to="/whatever" tag="li" exact class="app-navigation__list__item">
                    <a href="">
                        <span class="link-text">Home</span>
                    </a>
                </router-link>
                <router-link to="/whatever" tag="li" exact class="app-navigation__list__item">
                    <a href="">
                        <span class="link-text">About</span>
                    </a>
                </router-link>
                <router-link to="/whatever" tag="li" exact class="app-navigation__list__item">
                    <a href="">
                        <span class="link-text">Store</span>
                    </a>
                </router-link>
            </ul>
          </div>
      </b-collapse>
  </b-navbar>
</template>

答案 4 :(得分:0)

我知道这是一个老问题,但我的回答可能会对某人有所帮助。 在您的路由器/index.js 中执行此操作

ptrdiff_t

然后在每条路线进入之前添加这个。

    const router = createRouter({}); //P.S I am using Vue3

答案 5 :(得分:0)

    router.beforeEach((from,to,next) => {
           $('.navbar-collapse').collapse('hide'); //Be sure to import jquery
        next();
    });

这有效