一、vuex
1-1 获取vuex-state的数据
this.$store.state
1-2 this.$store.dispatch向vuex派发一个事件 —actions
methods:{ handleClick(city){ /* this.$store.dispatch 向vuex派发一个事件,同时传递一个参数 */ this.$store.dispatch("changeCity",city) }}
二、vuex的工作
1.通过this.$store.dispatch()向vuex中的actions派发一个事件同时传递一个参数methods:{ handleClick(city){ /* this.$store.dispatch 向vuex派发一个事件,同时传递一个参数 */ this.$store.dispatch("changeCity",city) }}2.vuex-actions中接收dispatch派发过来的事件和参数export default new Vuex.Store({ ... actions: { changeCity(ctx,city){ /* ctx表示上下文 this.$store city是自定义事件传递过来的参数 */ 3.在actions中使用commit方法向mutations提交一个事件,同时传递一个参数 ctx.commit("toggleCity",city) } }})
示意图:
