Answer the question
In order to leave comments, you need to log in
How to update computed property in VUE when Array element changes?
Good afternoon.
There is a computed prop- filterResultSelectionIds:
filterResultSelectionIds() {
let filterSelection = [];
this.filterResultSelection.forEach((group) => {
filterSelection.push(group);
});
filterSelection = [].concat(...filterSelection);
return filterSelection.map(el => el.id);
}
Answer the question
In order to leave comments, you need to log in
Computed properties always cache the value and update the cache only when the reactive data has changed. It does not update the array, unless its length has changed. So changing an array element will not trigger a computed property update. You can use watcher with option deep: true
I'll leave this here for those who are looking for an answer to a similar question.
In such situations, when we want to "update" the computed property when a non-reactive event occurs, it is sometimes very convenient to set a counter variable in data. When such an event occurs, we simply increase the counter.
In our target computed property, we use this variable as we like, for example like this:
data() {
return {
changed: 0,
myArray: []
}
},
computed: {
isArrayChanged() {
let hook = this.changed;
return (myArray)
}
},
methods: {
myArrayNewLenght(data) {
myArray.length = data; //
this.changed++;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question