I have an fileArray array to hold all the File Object in a Vue 2 app. When I update a property of an File Object using this.set as recommended, it will not re-render (to toggle "active" class). However, it would work if the array holds normal Object such as fileArray = [{name: "John"}, {name: "Jacob"}] but not the File Object
Code
<template>
<div v-for="(item, index) in fileArray">
<a-card :class="item.active ? 'active' : ''">
<img :src="item.imageUrl" @click="handleSelect(index)" />
</div>
</template>
export default {
data() {
return {
fileArray: []
}
},
methods: {
beforeUpload(file) {
// Add file object into array
this.fileArray.push(file)
return false
}
},
handleSelect(index) {
if (this.fileArray[index]) {
// Update object property inside an array with this.$set but it won't re-render
this.$set(this.fileArray[index], 'active', !this.fileArray[index].active)
}
}
}
- Some how the code below will help to re-render but spread operator doesn't work with File
beforeUpload(file) {
// Add file object into array
this.fileArray = [...this.fileArray, {...file}]
return false
}
},
- Temporary solution to re-assign the array so it will re-render
handleSelect(index) {
if (this.fileArray[index]) {
this.$set(this.fileArray[index], 'active', !this.fileArray[index].active)
this.fileArray = [...this.fileArray]
}
}
Please help if you experienced this.