Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

271
Vistas
index in v-for for array v-if not updating - Vue.js

I've created a simple vue.js program that uses a for loop to toggle the visibility of text. I have a button that should toggle it, but when it is clicked, it changes the variable but doesn't update the button.

let app = new Vue({
  data() {
    return {
      array: [true,true,false],
      text: ["0","1","2"]
    }
  },
  methods:{
    change(index){
      this.array[index]=!this.array[index]
      console.log(this.array[index],index)
    }
  },
  el: "#app"
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div v-for="(item,index) in text">
    <button @click="change(index)">toggle show</button>
    <a v-if="!array[index]">...</a>
    <a v-else>{{item}}</a>
  </div>
</div>

Is there any way I could get this simple app to work without changing the arrays?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You’re running into a reactivity caveat: you cannot update a specific entry in an array by index in that way.

You’ll need to use Vue.set or this.$set (the latter is simply an alias for the former):

change(index){
  this.$set(this.array, index, !this.array[index]);
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda