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

132
Visualizações
Vue js show hide element on click

I am new in vue js. I want to show hide elements on click. So that What I can do - make a property in data object like

isHidden: false

and add a mehtod like

showHide(){
return this.isHidden = !this.isHidden;
}

And bind the showHide method on click event like

<li @click="showHide">Some text</a>

But the problem is- if there are several list items binding the same click event, all lists will be shown on click, or all will hide. But I want the target element will only show or hide by click.

How can I do this, please?

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

0

You can use some data to determine what element you want show/hide. For example: id, index of each element

<button v-for="(i, ind) in listElement" @click="toggleElement(ind)" :key="ind">
  Toggle {{ i }}
</button>

<p v-for="(i, ind) in listElementShow" :key="ind">
  Element: {{ i }}
</p>

Logic:

  data() {
    return {
      listElement: [ { name: 1, isShow: true}, { name: 2, isShow: true}],
    }
  },
  computed: {
    listElementShow() {
      return this.listElement.filter(e => e.isShow); 
    }
  }
  methods: {
    toggleElement(index) {
      this.listElement[index].isShow = !this.listElement[index].isShow;
    }
  }
about 4 years ago · Juan Pablo Isaza Relatório

0

You can do something like this:

<template>
    <ul class="list-with-hidden-items">
        <li
            v-for="item in list"
            :key="item.id"
            @click="showHide(item.id)"
        >
            {{ item.text }}
        </li>
    </ul>
</template>

<script>
export default {
    data: () => ({
        hidden: {}
    }),
    methods: {
        showHide(id) {
            this.$set(this.hidden, id, !this.hidden[id]);
        }
    }
}
</script>

and then you can render where you want your item to be shown like this:

<div v-if="!this.hidden.item1">I am shown because of item1 is not hidden</div>
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a dynamically bound classObject to do such a thing. See Class and Style Bindings

Sandbox

<template>
  <div @click="showHide()" :class="{ isHidden: isHidden }">
    <p>This is a thing</p>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  data() {
    return {
      isHidden: false,
    };
  },
  methods: {
    showHide() {
      this.isHidden = !this.isHidden;
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.isHidden {
  background-color: grey;
}
div {
  background-color: #fff;
}
</style>
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