I'm using the following code to increment a counter in store.js using Vuex. Don't know why, when I click the increment button, it says:
[vuex] unknown action type: INCREMENT
store.js
import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)
var store = new Vuex.Store({
state: {
counter: 0
},
mutations: {
INCREMENT (state) {
state.counter++;
}
}
})
export default store
IcrementButton.vue
<template>
<button @click.prevent="activate">+1</button>
</template>
<script>
import store from '../store'
export default {
methods: {
activate () {
store.dispatch('INCREMENT');
}
}
}
</script>
<style>
</style>
You have to use
commit
in the methods as you are triggering a mutation, not an action:Actions are similar to mutations, the difference being that: