I just starts learning Vue.js (awesome framework), and I get into Vuex. I don't understand the real use-case why I should prefere using it.
I mean I have a simple counter (like in the Vue doc):
Why should I use the Vuex syntax:
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment: state => state.count++,
decrement: state => state.count--
}
})
new Vue({
el: '#app',
computed: {
count () {
return store.state.count
}
},
methods: {
increment () {
store.commit('increment')
},
decrement () {
store.commit('decrement')
}
}
})
While I can use:
class Store {
constructor(){
this.state = {
count: 0
}
}
/* mutations: { */
increment = state => this.state.count++;
decrement = state => this.state.count--;
/* } */
}
let store = new Store();
new Vue({
el: '#app',
data: {
state: store.state
},
computed: {
count () {
debugger
return this.state.count
}
},
methods: {
increment () {
debugger;
store.increment()
},
decrement () {
store.decrement()
}
}
})
Both works, and IMO the second one is much more simple??
Aucun commentaire:
Enregistrer un commentaire