vuex commit does not commit to store

五迷三道 提交于 2019-12-01 23:21:46

问题


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:


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.



来源:https://stackoverflow.com/questions/42294030/vuex-commit-does-not-commit-to-store

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!