Can't send in a second parameter to the mutation in Vue store

5.3k views Asked by At

I have a nice mutation JS file like this.

export default {
  UPDATE_DATA: (state, data, meta) => {
    state.dataTable = data;

    if (meta === undefined)
      return;

    state.activeDataRow = meta;
  }, ...
}

It's being called by to different actions in the following ways.

context.commit("UPDATE_DATA", Map.table(payload.type, data));
context.commit("UPDATE_DATA", Map.table(payload.type, data), meta);

I've checked the meta being sent in the action and it's definitely not undefined. However, when I check the meta in the mutation, it is! Why? How do I kill this problem?

1

There are 1 answers

3
Saurabh On BEST ANSWER

What vuex docs suggests is to send a payload in the second argument.

In most cases, the payload should be an object so that it can contain multiple fields, and the recorded mutation will also be more descriptive:

So you can call it like this with payload:

context.commit("UPDATE_DATA", {data: Map.table(payload.type, data), meta: meta});

and your mutation will look like following:

export default {
  UPDATE_DATA: (state, payload) => {
    state.dataTable = payload.data;

    if (payload.meta === undefined)
      return;

    state.activeDataRow = payload.meta;
  }, ...
}

There is an alternet way to calling mutations as well which is called Object-Style Commit. You can pass an object in commit, with type as mutation name, like following:

context.commit({
     type:: "UPDATE_DATA", 
     data: Map.table(payload.type, data), 
     meta: meta
});