'Replace array item in Vuex Nuxt [duplicate]
I need to replace some array items by index in vuex nuxtjs:
changeTicket(state, value) {
// 1 - Vue.set(state.tickets, value.index, value.numbers)
// 2 - state.tickets[value.index] = value.numbers
}
Im trying 2 variants, in first i have an error = [vuex] do not mutate vuex store state outside mutation handlers., in second im lost reactivity.
In component, im sending new array after click:
setNumber(num) {
const vResult = this.numbers;
if(vResult.includes(num)) {
vResult.splice(vResult.indexOf(num), 1)
} else {
if(vResult.length >= this.options[this.game].max) return;
vResult.push(num)
}
this.$nextTick(() => {
this.$store.commit("games/changeTicket", {
numbers: vResult,
index: this.index
})
})
},
Arrays example:
[
[1,2,3,4,5],
[2,5,61,21],
...
]
Solution 1:[1]
I guess changeTicket really is a Vuex mutation, right? Otherwise, that's why you get the Vuex warning.
In the mutation, just use splice in your array:
changeTicket(state, { index, numbers }) {
state.tickets.splice(index, 1, numbers)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kapcash |
