LoginSignup
9
8

More than 3 years have passed since last update.

Vue Router(Nuxt.js)で現在のページ(カレントページ)にclassを自動追加する方法

Last updated at Posted at 2020-01-03

Vue Routerで現在のページ(カレントページ)にclassを自動追加する方法

グローバルナビゲーションなどで、現在のページだけクラスを付けた時があると思います。
Vue Routerでは比較的簡単に実装できます。

      <nav>
        <ul class="gnav">
          <li><router-link to="/" active-class="active-list" exact>HOME</router-link></li>
          <li><router-link to="/drink" active-class="active-list">DRINK</router-link></li>
          <li><router-link to="/info" active-class="active-list">INFO</router-link></li>
          <li><router-link to="/contact" active-class="active-list">CONTACT</router-link></li>
        </ul>
      </nav>

上記のようにactive-classと言う属性を設定することで、そのディレクトリ内の時だけクラスを付けることができます。

しかし、これだと包含するディレクトリにもクラスがついてしまいます。(上記の例では、下位のディレクトリに移動してもトップページにもクラスがつく)

包含では無く完全に一致した時だけクラスを付けたい時はexactを記述します。
そうすることにより、下位のディレクトリに移動した時には、クラスが外れます。(上記の例ではトップページにexactを記述)

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8