【问题标题】:unknow mutation type usin Vuex使用 Vuex 的未知突变类型
【发布时间】:2019-07-02 20:50:01
【问题描述】:

组件

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 获得价值。但卡在这个错误上:[vuex] unknown mutation type。我错过了什么吗?

【问题讨论】:

  • 不是 vuex 专业人士,但您需要将 store.js 定义为 vuex 模块事件吗? const store = new Vuex.Store({ modules: { events: StoreData } });
  • 你的突变没有任何作用。你必须分配一个值。您可以将有效负载传递给您的突变。

标签: javascript vue.js vuejs2 vue-component vuex


【解决方案1】:

您应该为您的 state 属性分配一些值,如下所示:

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

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

问题根源

在您的 main.js 中,您应该使用 const store = new Vuex.Store(StoreData); 而不是 const store = new Vuex.Store({StoreData});,方法是在 StoreData 周围删除 {}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-13
    • 2019-10-07
    • 2018-08-17
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    相关资源
    最近更新 更多