1
1
100R2019-09-15 23:31:17
Vue.js
100R, 2019-09-15 23:31:17

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 idis 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 idit 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>

action

updateQuantity: ({commit}, lalala, id) => {
    commit(lalala, id);
}


Basket:

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

1 answer(s)
D
Dima Pautov, 2019-09-15
@100R

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 question

Ask a Question

731 491 924 answers to any question