3
votes

I'm having trouble getting my commit to work on a store.

I am calling the mutation via an action, which seems to work fine.

export const location = {
  state: {
    x: 0,
    y: 0,
    z: 0,
    extent: [],
    epsg: 'EPSG:3857'
  },
  mutations: {
    setLocation(state, payload) {
      // mutate state
      console.log("Commit");
      state.x = payload.x;
      state.y = payload.y;
      console.dir(state); //this does return the mutated state.
    }
  },
  actions: {
    setLocation(context, payload) {
      console.dir(payload);
      context.commit('setLocation', payload);
    }
  },
  getters: {
    mapLocation(state) {
      return state
    }
  }
}

The action is imported into my component:

methods: {
     ...mapActions(['setLocation']),

and then called:

var location = {
      x: data[0],
      y: data[1]
    }
    this.setLocation(location);

This all appears to work, but when I check out Vue Developer Tools, the Vuex Base state remains unchanged and I have an active mutation (setLocation). I can click 'Commit All' to commit the mutation which works.

In my component I am using a watcher on the getter mapLocation which fires when I click Commit All.

How can I force it to commit to the store?

Thanks

1

1 Answers

3
votes

Ok, this was actually a very simple problem/oversight.

I hadn't modelled anything in the DOM on the computed property I was watching, so it was never updated.

So the solution is simply to use v-model="mapLocation" to ensure the watch fires.