Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
How to add active class in this loop in which is dynamically showing the menus Vue Js?

How to add active class in this loop in which is dynamically showing the menus Vue Js? I couldn't place active class for the open menus.

 <a v-bind:href="module.route" class="nav-link">
     //module.route is a database column
      <i :class="module.icon" ></i>
        //module.icons is a database column
       <p>
         {{ module.name }}
            //module.name is a database column
       </p>
  </a>

Its showing the list of menus from database .Now I just want to make the open menu active.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I did it this way.

<!-- App.vue HTML PART -->
<li v-for = "(tab, index) in tabs" :key="index" class="nav-link" @click="active_tab = tab" v-bind:class="{ 'active' : tab === active_tab }">
    ...
</li>

I added all the tabs I wanted to add in an array named tabs

<script>

export default {
  name: 'Home',

  data() {
    return {
      active_tab: "Tab1",
      tabs: ["Tab1", "Tab2", "Tab3"]
    }
}

</script>

v-for = "(tab, index) in tabs"

This is going to create one <li> for every tab in the array tabs

class="nav-link" @click="active_tab = tab" v-bind:class="{ 'active' : tab === active_tab }"

All the items are going to have a class of nav-link and on top of that with v-bind, it's going to add active to the element's classlist if the variable active_tab is equal to tab. And everytime the user clicks on one of the tabs, with @click it'll change active_tab to the tab that the user clicked.

Styling

.nav-link {
    background-color: var(--primary-color);
    /* how the element should look when it is normal */
}
.nav-link.active {
    background-color: var(--hover-color);
    /* when it is active */
} 
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda