Answer the question
In order to leave comments, you need to log in
What is the correct way to change data inside an array when using Vuex?
Good day.
There is vuex, which has a variable that stores an array of objects. We need to make it possible to change objects inside the array and all this would be immediately reflected in the Vue component. In the component itself, something like this code
computed:{
view(){
return this.$store.getters.getView;
}
}
EDIT_VIEW_LIST(state, item){
state.listView.forEach((elem,idx)=>{
if(elem.id===item.id){
elem=item;
}
})
}
EDIT_VIEW_LIST(state, item){
let idx=state.listView.findIndex((elem,idx)=>{
return elem.id===item.id;
});
if(idx){
state.listView[idx]=item;
}
}
EDIT_VIEW_LIST(state, item){
state.listView=state.listView.map((elem,idx)=>{
return elem.id===item.id ? item : elem;
})
}
Answer the question
In order to leave comments, you need to log in
Here is a description for working with arrays: https://ru.vuejs.org/v2/guide/list.html#Tracked...
The method should helpVue.set
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question