E
E
Eugene2020-10-24 10:15:40
Vue.js
Eugene, 2020-10-24 10:15:40

How to make active class only on one menu item?

There is this code:

<template>
   <ul class="sidenav app-sidenav open">
      <li v-for="link in links" :key="link.url">
         <router-link
            :to="link.url"
            class="waves-effect waves-orange pointer"
         >
            {{ link.title }}
         </router-link>
      </li>
   </ul>
</template>

<script>
export default {
   data: () => ({
      links: [
         { title: "Счет", url: "/" },
         { title: "История", url: "/history" },
         { title: "Планирование", url: "/planning" },
         { title: "Новая запись", url: "/record" },
         { title: "Категории", url: "/categories" },
      ],
   }),
};
</script>

It is necessary that only one menu item is assigned the class active and other items do not have it.
help me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-10-24
@Eugene07

Such a class already exists . If you need to style the link - use it .
UPD. Taken from the comments:

Is there a way to assign your own class?

Somehow you can :
computed: {
  activeLink() {
    return this.links.find(n => n.url === this.$route.path);
  },
},

:class="{ active: activeLink === link }"
One thing is not clear - why?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question