Search code examples
javascriptvue.jsvuejs2vue-componentvuex

unknow mutation type usin Vuex


component

computed: {
   score () {
    this.$store.commit('geolocation');
    console.log(this.$store.getters.lat);
   }
}

store.js

export default {
  state: {
   lat:'ok',
  },
  getters:{
    cordinate(state){
    return state.lat
    }
  },  
  mutations: {
    geolocation(state){
     state.lat
    }
  },
  actions: {
  }
}

App.js

import StoreData from'./store.js';
Vue.use(Vuex);
const store = new Vuex.Store({StoreData});
new Vue({
  el: '#app',
  data: {},
  router: router,
   store,
})

New to vuex. Tried to get value from vuex . but stuck on this error:[vuex] unknown mutation type. am i missing something ?


Solution

  • You should assign some value to your state property as follows:

    computed: {
       score () {
        this.$store.commit('geolocation',78);
        console.log(this.$store.getters.lat);
       }
    }
    
      mutations: {
        geolocation(state,newLat){
         state.lat=newLat;
        }
      }
    

    when using Vuex store it is recommended to use mutations and actions inside methods and getters inside computed properties.

    The source of issue

    In your main.js you should have const store = new Vuex.Store(StoreData); instead of const store = new Vuex.Store({StoreData}); by removing {} around StoreData