In Vue 1.x we have track-by="$index" to track changed index in rendered array. but since version 2.x, Vue suggest using :key when we rendering array in view with v-for intead track-by="$index". but consider this blow example:
In <template> :
<div v-for="(doc, i) in docs" :key="i">
<h4>{{ doc.status }}</h4>
<button @click="reject(i)"> Reject </button>
</div>
In <script> :
data: {
docs: [
{ status: 'pending' },
{ status: 'pending' },
{ status: 'pending' }
]
},
methods: {
reject(index) {
this.docs[index] = { status: 'rejected' }
}
}
in this example, when we change an index, Although array changes but we can't see any change in view. that's because our component rendered before and we need to update its view. for this we need to use this.$forceUpdate() in our method to update the component.
reject(index) {
this.docs[index] = { status: 'rejected' }
this.$forceUpdate();
}