最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't send in a second parameter to the mutation in Vue store - Stack Overflow

programmeradmin3浏览0评论

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.

contextmit("UPDATE_DATA", Map.table(payload.type, data));
contextmit("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?

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.mit("UPDATE_DATA", Map.table(payload.type, data));
context.mit("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?

Share Improve this question edited Dec 18, 2016 at 0:47 Saurabh 73.7k44 gold badges191 silver badges251 bronze badges asked Dec 17, 2016 at 18:33 Konrad VilterstenKonrad Viltersten 39.3k85 gold badges287 silver badges509 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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.mit("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 mit, with type as mutation name, like following:

context.mit({
     type:: "UPDATE_DATA", 
     data: Map.table(payload.type, data), 
     meta: meta
});
发布评论

评论列表(0)

  1. 暂无评论