简体   繁体   English

从 VUEX 中的突变中改变特定状态

[英]Mutating a specific piece of state from the mutation in VUEX

I'm having a hard time doing this and I'm sure it's simple but I can't get it to work.我很难做到这一点,我相信这很简单,但我无法让它发挥作用。 I have a toggle switch with boolean value that I am successfully making it work from the Vue file but obviously vuex is yelling cause any prop change needs to be mutated in the vuex file.我有一个带有布尔值的切换开关,我成功地使它从 Vue 文件中工作,但显然 vuex 正在大喊大叫,导致任何 prop 更改都需要在 vuex 文件中进行突变。 Here is the relevant code:这是相关的代码:

Vue file Vue文件

<template>
   <workstation
      v-for="(workstation, index) in hStation.workstations" :key="index"
      :id="workstation.recordId"
      :close="workstation.closed"
      @toggledState="toggleState(workstation)"
      ></workstation>
</template>

<script>
   methods: {
     ...mapActions("pod", ["updateWorkstation"]),
     toggleState(workstation) {
        workstation.closed = !workstation.closed;
        this.updateWorkstation({
           recordId: workstation.recordId,
           closed: workstation.closed
        })
        .then(response => {
            console.log("id: ", workstation.recordId);
            console.log("closed: ", workstation.closed);
        })
        .catch(error => {
            console.log("error: ", error);
        });
     },
   },
</script>

The vuex file simplified简化的 vuex 文件

import { axiosInstance } from "boot/axios";

export default {
  namespaced: true,
  state: {
    workstation: []
  },
  getters: {
    singleWorkstation: state => {
      return state.workstation;
    }
  },
  actions: {
    updateWorkstation: ({ commit }, payload) => {
      return new Promise((resolve, reject) => {
        axiosInstance
          .post("Workstation/update", payload)
          .then(({ data, status }) => {
            if (status === 200) {
              resolve(true);
              commit("setWorkstation", data.data);
            }
          })
          .catch(({ error }) => {
            reject(error);
          });
      });
    }
  },
  mutations: {
    setWorkstation: (state, workstation) => (state.workstation = workstation)
  }
};

Error: [vuex] do not mutate vuex store state outside mutation handlers.错误:[vuex] 不要在变异处理程序之外改变 vuex 存储状态。

API schema API 架构

{
  "success": true,
  "data": [
    {
      "recordId": 0,
      "worksite": 0,
      "hStations": [
        {
          "recordId": 0,
          "podId": 0,
          "stationOrder": 0,
          "workstations": [
            {
              "recordId": 0,
              "name": "string",
              "closed": true,
            }
          ]
        }
      ]
    }
  ]
}

How do I fire the change on the close property within the mutation?如何触发突变中 close 属性的更改? Thanks in advance提前致谢

Instead of passing a new object to the action, pass the whole workstation object.不是将新对象传递给操作,而是传递整个workstation对象。

this.updateWorkstation(workstation);

You'll create the posting object, postdata , inside the action, and you'll commit a second mutation for toggling when the promise resolves:您将在操作中创建发布对象postdata ,并且您将在承诺解决时提交第二个postdata以进行切换:

updateWorkstation: ({ commit }, workstation) => {
  const postdata = {
    recordId: workstation.recordId,
    closed: workstation.closed
  }
  return new Promise((resolve, reject) => {
    axiosInstance
      .post("Workstation/update", postdata)
      .then(({ data, status }) => {
        if (status === 200) {
          resolve(true);
          commit("setWorkstation", data.data);
          commit("toggleOldWorkstation", workstation);
        }
      })
      .catch(({ error }) => {
        reject(error);
      });
  });
}

Since the workstation is in the action this way, you're able to call that second mutation to toggle the closed property:由于workstation以这种方式运行,您可以调用第二个突变来切换closed属性:

mutations: {
  ...
  toggleOldWorkstation(workstation){
    workstation.closed = !workstation.closed;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM