无法将第二个参数发送到 Vue 商店中的突变 [英] Can't send in a second parameter to the mutation in Vue store

查看:17
本文介绍了无法将第二个参数发送到 Vue 商店中的突变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的不错的突变 JS 文件.

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?

推荐答案

什么 vuex docs 建议在第二个参数中发送有效载荷.

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

在大多数情况下,payload应该是一个对象,这样它可以包含多个字段,并且记录的mutation也会更具描述性:

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:

所以你可以用payload这样调用它:

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
});

这篇关于无法将第二个参数发送到 Vue 商店中的突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆