一、vuex

1-1 获取vuex-state的数据

  1. this.$store.state

1-2 this.$store.dispatch向vuex派发一个事件 —actions

  1. methods:{
  2. handleClick(city){
  3. /* this.$store.dispatch 向vuex派发一个事件,同时传递一个参数 */
  4. this.$store.dispatch("changeCity",city)
  5. }
  6. }

二、vuex的工作

  1. 1.通过this.$store.dispatch()向vuex中的actions派发一个事件同时传递一个参数
  2. methods:{
  3. handleClick(city){
  4. /* this.$store.dispatch 向vuex派发一个事件,同时传递一个参数 */
  5. this.$store.dispatch("changeCity",city)
  6. }
  7. }
  8. 2.vuex-actions中接收dispatch派发过来的事件和参数
  9. export default new Vuex.Store({
  10. ...
  11. actions: {
  12. changeCity(ctx,city){
  13. /* ctx表示上下文 this.$store
  14. city是自定义事件传递过来的参数 */
  15. 3.actions中使用commit方法向mutations提交一个事件,同时传递一个参数
  16. ctx.commit("toggleCity",city)
  17. }
  18. }
  19. })

示意图:

vuex - 图1