Answer the question
In order to leave comments, you need to log in
Why is the second parameter not passed to action?
Why is the second parameter not passed to action? If you remove the first parameter, then it id
is passed and everything works. As soon as I put two, then id
= undefined
.
With buttons, everything is clear. How to be with yourself input
? The example I found in the documentation doesn't work when an action is also passed in id
. That is, e.target.value
= undefined
. Without id
it works fine.
Component
<div class="quantity">
<button @click="updateQuantity('increment', item.id)">+</button>
<input type="number" :value="item.quantity">
<button @click="updateQuantity('decrement', item.id)">-</button>
</div>
updateQuantity: ({commit}, lalala, id) => {
commit(lalala, id);
}
export default {
namespaced: true,
strict: true,
state: {
items: [
{
id: 0,
quantity: 1
},
{
id: 1,
quantity: 1
}
]
},
mutations: {
add: (state, id) => {
state.items.push(
{
id,
quantity: 1
}
);
},
remove: (state, id) => {
let index = state.items.findIndex(item => item.id === id);
state.items.splice(index, 1);
},
increment: (state, id) => {
let item = state.items.find(item => item.id === id);
item.quantity++;
},
decrement: (state, id) => {
let item = state.items.find(item => item.id === id);
item.quantity--;
},
},
actions: {
addOrRemove: ({state, commit}, id) => {
let item = state.items.find(item => item.id === id);
if (!item) {
commit('add', id);
} else {
commit('remove', id);
}
},
updateQuantity: ({commit}, lalala, id) => {
commit(lalala, id);
}
}
}
Answer the question
In order to leave comments, you need to log in
Because actions can't take 100500 parameters. The parameter must be 1 - an object with 100500 properties.
PS I don't know why this is happening, it's a bug or a feature, but it's true. I did not climb into the outback and did not study. I have only guesses related to the fact that the 1st argument is always reserved for the store. Yes, and the objects look better, you don’t need to follow the order and you can display them just as conveniently as the arguments
updateQuantity: ({commit}, {lalala, id}) => {
commit('setData', {lalala, id});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question