【问题标题】:Mutating a specific piece of state from the mutation in VUEX从 VUEX 中的突变中改变特定的状态
【发布时间】:2020-07-06 11:33:45
【问题描述】:

我很难做到这一点,我确信这很简单,但我无法让它发挥作用。我有一个带有布尔值的拨动开关,我成功地使其从 Vue 文件中工作,但显然 vuex 大喊大叫,因为任何道具更改都需要在 vuex 文件中进行突变。以下是相关代码:

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>

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

错误:[vuex] 不在突变处理程序之外改变 vuex 存储状态。

API 架构

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

如何触发突变中 close 属性的更改?提前致谢

【问题讨论】:

    标签: javascript vue.js vuex


    【解决方案1】:

    不要将新对象传递给动作,而是传递整个workstation 对象。

    this.updateWorkstation(workstation);
    

    您将在操作中创建发布对象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);
          });
      });
    }
    

    由于workstation 以这种方式运行,您可以调用第二个突变来切换closed 属性:

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

    【讨论】:

    • 它没有用。 :( 仍然是一个更好的方法和书面,但我仍然收到一条消息,不要在突变处理程序之外改变 vuex 存储
    • 由于这是显示的代码中唯一更改的值并且它在处理程序中处理,因此必须有第二个未显示的错误突变,可能在 workstation 组件内
    猜你喜欢
    • 2021-09-16
    • 2020-03-08
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多