The problem is not v-if
, because Vue cannot directly detect changes to an array element, this is one of the limitations of JavaScript.
Thus, Vue provides some helper functions for this, for example Vue.set
Change this this.show[index] = !this.show[index]
to Vue.set(this.show, index, !this.show[index])
then it should work.
Vue.set
is not the only solution, there are several ways to do this if you want to find out.
You can use your own methods for the JavaScript array, Vue will use these methods so that it can detect changes.
Here is an example using .splice
this.show.splice(index, 1, !this.show[index])
Another way is to completely replace the array. If you are using ES6, you can use the spread operator to easily clone the array:
this.show[index] = !this.show[index] this.show = [...this.show]
.map
will also work because it returns a new array
this.show = this.show.map((el, i) => i === index ? !el : el )
source share