1

I cannot get either a mutation or action to pass data to the app state. I'm not even sure if I'm calling them correctly, despite having spent a couple of hours reading the Vuex docs.

I started out creating a mutation to simply update my state when an @click event is fired, but when it didn't work, I tried an action and got the same result: nothing.

Example of how I'm calling the action:

<div v-for="(data, index) in dataList" :key="index">
  <div @click="updateDataSomewhereElse(data)">  // action called here
    // do some other stuff
  </div>
</div>

Example of how I'm dispatching the action from my single-file component:

// some code
methods: {
  updateDataSomewhereElse: function(data) {
    this.$store.dispatch('setData', data); // action dispatched here
  }
}

Example of my action and mutation:

// state code
mutations: {
  updateStateData(state, data) {
    state.data = data;
  }
},
actions: {
  setData({commit}, data) {
    commit('updateStateData', data);
  }
}

Needless to say, I'm expecting my state to be updated with my mutation when my action gets called, but I'm not getting anything.

What have I done wrong?

Edit - More Details:

Example of my initial state:

state: {
  data: 'something'
}

From my main.js:

import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app');

I know it's not returning anything in my Vue devtools as it only shows my initial state and I can't do a manual commit/dispatch from there. Also, console.log statements in both my method and my action aren't being triggered. I've got my store.js file connected to Vue with Vue.use(Vuex);, so it's not an issue of connection.

jstrother
  • 190
  • 1
  • 3
  • 17
  • Can you double check the following: - store is added to the Vue instance - debug by adding console log to the method to verify that is called - debug to verify that the action is called – Simon Thiel Apr 03 '19 at 23:37
  • 1
    How are you verifying the problem? What are you expecting to happen and what is actually happening? Please also provide your initial `state` – Phil Apr 03 '19 at 23:57
  • I suspect this is a duplicate of [Vuex Mutation running, but component not updating until manual commit in vue dev tools](https://stackoverflow.com/questions/52865612/vuex-mutation-running-but-component-not-updating-until-manual-commit-in-vue-dev) but will wait for confirmation – Phil Apr 03 '19 at 23:58
  • Do you have `Vue.use(Vuex)` anywhere? – Phil Apr 04 '19 at 01:29
  • Yes, I have it in my store.js file. – jstrother Apr 04 '19 at 01:31
  • _"console.log statements in both **my method** and my action aren't being triggered"_ wait, in your **method**? Are you clicking on the `
    `? Do you have any other click handler on any child elements that might be using `@click.stop`?
    – Phil Apr 04 '19 at 01:32
  • Yeah, I'm clicking on the ```
    ``` and that is the only ```@click``` that I have on the element.
    – jstrother Apr 04 '19 at 01:35

1 Answers1

0

I fixed this issue by simply doing this:

<div v-for="(data, index) in dataList" :key="index">
  <div>
    <span @click="updateDataSomewhereElse(data)">do some other stuff</span>
  </div>
</div>

I'm not sure why or how this is the answer, but I'm now getting my state to update.

jstrother
  • 190
  • 1
  • 3
  • 17