I have the following code below using Vue.js and Vuex . This code increments and decrements value as a counter. I would like to know the count
method within computed in the Vue instance. I know it returns the count status of state
. But why does the method name have to be the name of the variable? If I change the method name the code does not work.
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
incrementa: state => state.count++,
decrementa: state => state.count--
}
})
new Vue({
el: '#app',
computed: {
count() { // Dúvida -> ao que se refere count
return store.state.count
}
},
methods: {
incrementa() {
store.commit('incrementa')
},
decrementa() {
store.commit('decrementa')
}
}
})
span{padding: 0 15px;}
<script src="https://unpkg.com/[email protected]/dist/vuex.js"></script><scriptsrc="https://unpkg.com/[email protected]/dist/vue.js"></script>
<div id="app">
<p>
<button @click="incrementa">+</button>
<span>{{ count }}</span>
<button @click="decrementa">-</button>
</p>
</div>