如何通过多个变异来更新Vuex存储?

时间:2019-02-09 13:38:51

标签: vue.js state vuex c3.js

如果我仅对状态进行一次突变(通过提交以下所示的两个突变之一),则不会出现错误,并且图表会正确更新。 如果我同时运行两个突变:

commit('SET_COURSE_MATERIAL', data)
commit('SET_TOOLS_EQUIPMENT', data)

然后我得到Maximum call stack exceeded: RangeError

如果我注释掉chart.vue的watch属性中的代码,则没有错误,并且可以在console.log中看到具有正确值的状态

仅当我运行“ npm run dev”时,我才收到有关最大调用堆栈的错误。当我将其部署到Google Cloud时,该站点可以正常运行,并且没有任何错误。我什至通过编辑一些代码并重新部署两次来重新检查这一点,同时还注意到了构建日志中的时间。

summary.vue

        <v-card-text>
          <chart
            :chart-config.sync="this.$store.state.summary.courseMaterial"
          />
        </v-card-text>
...
        <v-card-text>
          <chart
            :chart-config.sync="this.$store.state.summary.toolsEquipment"
          />
        </v-card-text>

chart.vue

<template>
  <v-flex>
    <no-ssr><vue-c3 :handler="handler"/></no-ssr>
  </v-flex>
</template>

<script>
import Vue from 'vue'
export default {
  name: 'chart',
  props: ['chartConfig'],
  data() {
    return {
      handler: new Vue()
    }
  },
  watch: {
    chartConfig: function(val) {
      console.log('chart component > watch > chartConfig', val)
      this.drawChart()
    }
  },
  created() {
    this.drawChart()
  },
  methods: {
    drawChart() {
      this.handler.$emit('init', this.chartConfig)
    }
  }
}
</script>

store/summary.js

    import axios from 'axios'
import _ from 'underscore'
import Vue from 'vue'
import {
  courseMaterialChartConfig,
  toolsEquipmentChartConfig,
} from './helpers/summary.js'

axios.defaults.baseURL = process.env.BASE_URL

Object.filter = (obj, predicate) =>
  Object.assign(
    ...Object.keys(obj)
      .filter(key => predicate(obj[key]))
      .map(key => ({ [key]: obj[key] }))
  )

export const state = () => ({

  courseMaterial: '',
  toolsEquipment: '',

})

export const getters = {
  courseMaterial(state) {
    return state.courseMaterial
  },
  toolsEquipment(state) {
    return state.toolsEquipment
  }
}

export const actions = {
    async fetchData({ state, commit, rootState, dispatch }, payload) {
    axios.defaults.baseURL = process.env.BASE_URL
    let { data: initialData } = await axios.post(
      'summary/fetchInitialData',
      payload
    )
    console.log('initialData', initialData)
    let [counterData, pieChartData, vtvcData, guestFieldData] = initialData

    //dispatch('setCourseMaterial', pieChartData.coureMaterialStatus)
    //dispatch('setToolsEquipment', pieChartData.toolsEquipmentStatus)
  },
  setCourseMaterial({ commit }, data) {
    commit('SET_COURSE_MATERIAL', courseMaterialChartConfig(data))
  },
  setToolsEquipment({ commit }, data) {
    commit('SET_TOOLS_EQUIPMENT', toolsEquipmentChartConfig(data))
  }
}

export const mutations = {
  // mutations to set user in state
  SET_COURSE_MATERIAL(state, courseMaterial) {
    console.log('[STORE MUTATIONS] - SET_COURSEMATERIAL:', courseMaterial)
    state.courseMaterial = courseMaterial
  },
  SET_TOOLS_EQUIPMENT(state, toolsEquipment) {
    console.log('[STORE MUTATIONS] - SET_TOOLSEQUIPMENT:', toolsEquipment)
    state.toolsEquipment = toolsEquipment
  },
}

helpers/summary.js

export const courseMaterialChartConfig = data => {
  return {
    data: {
      type: 'pie',
      json: data,
      names: {
        received: 'Received',
        notReceived: 'Not Received',
        notReported: 'Not Reported'
      }
    },
    title: {
      text: 'Classes',
      position: 'right'
    },
    legend: {
      position: 'right'
    },
    size: {
      height: 200
    }
  }
}
export const toolsEquipmentChartConfig = data => {
  return {
    data: {
      type: 'pie',
      json: data,
      names: {
        received: 'Received',
        notReceived: 'Not Received',
        notReported: 'Not Reported'
      }
    },
    title: {
      text: 'Job Role Units',
      position: 'right'
    },
    legend: {
      position: 'right'
    },
    size: {
      height: 200
    }
  }
}

1 个答案:

答案 0 :(得分:0)

深复制图表配置。

# Switch to the current org's admin identity.  Enroll if not previously enrolled.
function switchToAdminIdentity {
   if [ ! -d $ORG_ADMIN_HOME ]; then
      dowait "$CA_NAME to start" 60 $CA_LOGFILE $CA_CHAINFILE