Can vue trigger an update function based on a key value in the data?

requirements:

  • renders a list of {{a}} using v-for. The data of the instance looks like
    item: [{a:false}, {a:true}, {a:false}, {a:true}] .
  • when the value of an in an element in data changes, a function is triggered

question:

  • can I get the index if I use watch to detect which data in item has changed?
  • is it possible to use computed?
Mar.07,2021

you can watch the entire item.

computed, you can return an array of updated results and then v-for the component.

provide a rough idea, ha

computed:{
    value(){
        return this.item.map((item)=>{
            if(item){
                //
               return update()
            } else {
                return <>
            }
        })
    }
}

then vFF = "value"


you can either watch or computed.
watch requires deep watch:

watch: {
    list: {
        deep: true,
        handler( newList, oldList){
            const changedIndex = newList.findIndex((item, index)=> {
                oldList[index] !== item // 
            })
            // changedIndex 
        }
    }
}

computed needs to calculate the item you need to listen to, and then watch it. It is suitable for you to know exactly which item you want to listen to. It has better performance than the above, after all, there are fewer listeners.

Menu