gpt4 book ai didi

javascript - 使用 Vuex 的未知突变类型

转载 作者:搜寻专家 更新时间:2023-10-30 22:24:44 25 4
gpt4 key购买 nike

组件

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,
})

vuex 新手。试图从 vuex 获取值(value)。但坚持这个错误:[vuex] 未知突变类型。我错过了什么吗?

最佳答案

您应该按如下方式为状态属性分配一些值:

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

当使用 Vuex 存储时,建议在方法中使用 mutationsactions,在计算属性中使用 getter。

问题来源

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

关于javascript - 使用 Vuex 的未知突变类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54595332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com