A
A
amanDev2019-05-14 19:57:45
Vue.js
amanDev, 2019-05-14 19:57:45

When I add a new contact, I get an error [vuex] unknown action type: ADD_CONTACT?

here is my method:

addContact(payload) {
                this.$store.dispatch({type:'ADD_CONTACT'}, payload)
            }

action and mutation:
mutations:{
        ADD_CONTACT:(state,payload)=> {
            state.contacts.push(payload)
        }
    },
    actions:{
        addContact:(context,payload)=> {
            context.commit({type:'ADD_CONTACT'}, payload)
        }
    },

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-05-14
@amanDev

this.$store.dispatch({type:'ADD_CONTACT'}, payload)

actions:{
    addContact:

Cool. Do you really not see the difference between ADD_CONTACT and addContact?
And if you use object syntax when calling an action (this also applies to mutations), payload should not be passed as a separate parameter, but as part of the first one:
this.$store.dispatch({
  type: 'addContact',
  payload,
});

actions: {
  addContact: ({ commit }, { payload }) => commit('ADD_CONTACT', payload),
},

Well, it’s not very clear why an action is needed here at all, you don’t perform any asynchronous operations, you can immediately call a mutation: . this.$store.commit('ADD_CONTACT', payload)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question