vuex

作者: guoss | 来源:发表于2018-06-29 13:39 被阅读0次

vuex是vue中的状态管理管理模式


image.png

公共的变量存放在state中,批量异步数据发生改变通过dispatch触发相应的action,然后通过commit触发mutations进而数据变化;也可以通过mutations进行同步操作触发数据变化。
store文件夹下的index.js文件

第一种情况

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state:{
        city:'北京'
    },
    actions:{
        changeCity(ctx,city){
            ctx.commit('changeCity',city)
        }
    },
    mutations:{
        changeCity(state,city){
            state.city=city
        }
    }
})

触发数据变化的页面

<div class="button-wrapper" v-for="item of hot" :key="item.id" @click="handleCityClick(item.name)">
    <div class="button">{{item.name}}</div>
</div>
methods:{
  handleCityClick(city){
    this.$store.dispatch('changeCity',city)
  } 
}//通过dispatch触发action中的changeCity,同时将city传入,触发commit方法,将变化数据city修改到state中

第二种情况

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state:{
        city:'北京'
    },
    mutations:{
        changeCity(state,city){
            state.city=city
        }
    }
})

触发数据变化的过程

methods:{
  handleCityClick(city){
    this.$store.commit('changeCity',city)
  }
},

相关文章

  • VUEX基本介绍,包含实战示例及代码(基于Vue2.X)

    VUEX 1 VUEX基本介绍 1.1 官方API 1.2 什么是vuex 1.3 Vuex使用场景 1、Vuex...

  • 【文档笔记】-Vuex

    什么是vuex? vuex文档: https://vuex.vuejs.org/zh/[https://vuex....

  • vuex配置

    vuex配置 目录 vuex的五个核心 配置vuex vuex持久化 一、vuex五个核心概念 State 定义状...

  • Vuex

    安装Vuex cnpm install vuex --save-dev Vuex是什么? 这是[Vuex的官网](...

  • Vuex

    1.Vuex概述 2.Vuex基本使用 3.使用Vuex完成todo案例 1.Vuex概述 Vuex是实现组件全局...

  • vuex

    Vuex介绍: Vuex官网:http://vuex.vuejs.org/ Vuex是实现数据状态管理的技...

  • vuex+axios 的开发流程记录

    相关文档 vuex: https://vuex.vuejs.org/zh/ 是否有必要使用vuex vuex是vu...

  • 2019-06-07

    import Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)...

  • 配置 vuex 和 vuex 本地持久化

    配置 vuex 和 vuex 本地持久化 目录 vuex是什么 vuex 的五个核心概念State 定义状态(变量...

  • vuex

    配置 vuex 和 vuex 本地持久化 目录 vuex是什么 vuex 的五个核心概念State 定义状态(变量...

网友评论

      本文标题:vuex

      本文链接:https://www.haomeiwen.com/subject/egvoyftx.html