const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment (state) {
state.count++
}
},
actions: {
increment (context) {
context.commit('increment')
}
}
})
store 中的状态的唯一方法是提交 mutation
store.commit('increment')
---------------------------
Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象,因此你可以调用 context.commit 提交一个 mutation [context 对象不是 store 实例本身了]
actions: {
increment ({ commit }) {
commit('increment')
}
}
----------------------------------------------------------
this.$store.dispatch()
与 this.$store.commit()
方法的区别总的来说他们只是存取方式的不同,两个方法都是传值给vuex的mutation改变statethis.$store.dispatch()
:含有异步操作,例如向后台提交数据,写法&