Reputation: 453
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 ?
Upvotes: 3
Views: 3074
Reputation: 1
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 haveconst store = new Vuex.Store(StoreData);
instead ofby removingconst store = new Vuex.Store({StoreData});
{}
aroundStoreData
Upvotes: 3