In the snippet below I have two Vue components, each with an items array containing two objects. Each component's template is set up to show an object of the items array if that object's show property is truthy. The first object in the array initially has a show value of true. The second object does not have a show value set.
new Vue({
el: '#ex1',
data: () => ({ items: [{name: 'apple', show: true}, {name: 'banana'}] }),
mounted() {
// 2nd item's show property setting is not detected here
this.items[1].show = true;
}
})
new Vue({
el: '#ex2',
data: () => ({ items: [{name: 'apple', show: true}, {name: 'banana'}] }),
mounted() {
// 2nd item's show property setting IS detected...
this.items[1].show = true;
// ...when the 1st item's show property is set to false
this.items[0].show = false;
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.min.js"></script>
<div id="ex1">
Example 1:
<div v-if="items[0].show">{{ items[0] }}</div>
<div v-if="items[1].show">{{ items[1] }}</div>
</div>
<div id="ex2">
Example 2:
<div v-if="items[0].show">{{ items[0] }}</div>
<div v-if="items[1].show">{{ items[1] }}</div>
</div>
In the first example, I set the show property of the second object in the items array to true. Vue does not detect the change in the template, which is expected.
In the second example, I set the show property of the second object to true, and then set the show property of the first object to be false. In this case, Vue does detect the change in both elements of the items array and updates the template accordingly. I would not expect this to be the case, given Vue's Change Detection Caveats.
This seems like a pretty innocuous bug (if it even qualifies as a bug), so I'm not sure if it's worth reporting. But, I was wondering if anyone would be able to explain this behavior. Why does Vue detect the addition of the show property to the object in the second example?