我正在使用 Vue 和 Vuex 编写一个 Electron 应用程序。
我的店铺如下(counter.js
):
const state = {
main: 5
};
const mutations = { // synchronous tasks
INCREMENT_MAIN_COUNTER (state) {
state.main++;
}
};
const getters = {
count: (state) => {
return state.main;
}
};
export default {
state, getters, mutations
}
我的 Vue 组件如下(LandingPage.vue
):
<template>
<div id="count-box">
{{count}}
<button @click="pressed">Increment counter</button>
</div>
</template>
<script>
import counter from '../store';
export default {
name: 'landing-page',
computed: {
count: () => {
return counter.getters.count;
}
},
methods: {
pressed: () => {
counter.commit('INCREMENT_MAIN_COUNTER');
}
}
};
</script>
当我单击按钮进行递增时,将commit
调用 ,并触发以下异常:
Uncaught Error: [Vuex Electron] Please, don't use direct commit's, use dispatch instead of this.
at Store.store.commit (.../node_modules/vuex-electron/dist/shared-mutations.js:1)
at VueComponent.pressed (LandingPage.vue?b116:20)
at invoker (vue.esm.js?a026:2027)
at HTMLButtonElement.fn._withTask.fn._withTask (vue.esm.js?a026:1826)
我不明白究竟是什么原因造成的,因为我一直在关注出现的https://www.youtube.com/watch?v=LW9yIR4GoVU和https://vuex.vuejs.org/guide/mutations.html这样做。