
Vuex是一个专为Vue.js应用程序开发的状态管理模式
它采用采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。
其实就是将其看成把需要多个组件共享的变量全部存储在一个对象里面
将这个对象放在顶层的Vue实例中,让其他组件可以使用
State:状态(可以看作data中的属性)
View:视图层,可以针对State的变化,显示不同的信息
Actions:主要是用户的各种操作:点击、输入等,会导致状态的改变

异步请求一般在actions中操作,mustations中一般只进行同步的操作,异步的操作devtools捕获不到。
注意事项:
类似于计算属性 computed
Vuex的store状态的更新唯一方式:提交Mutation
Mutation主要包括两部分:
参数被称为是mutation的载荷(Payload)
HelloWorld.vue
<template>
  <div class="hello">
  
    <h2>{{$store.state.counter}}</h2>
    <button @click="subs">-1</button>  
    <button @click="add">+1</button>  
    <button @click="addCount(5)">+5</button>  
    <button @click="addCount(10)">+10</button>
    <h2>平方为:{{$store.getters.getTest}}</h2>
  </div>
</template>
<script>
export default {
  name: 'HelloWorld',
  methods: {
    add () {
      this.$store.commit('increment')
    },
    subs () {
      this.$store.commit('sub')
    },
    addCount (count) {
      this.$store.commit('addCount', count)
    }
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
store/index.js
    mutations: {
        increment(state) {
            state.counter++
        },
        sub(state) {
            state.counter--
        },
        addCount(state, count) {
            state.counter += count
        }
    }
当传入多个参数时,可以使用对象的形式。。
上面的通过commit进行提交是一种普通的方式
Vue还提供了另外一种风格,它是一个type属性的对象
    addCount (count) {
      // this.$store.commit('addCount', count)
      /* 另一种提交的方式 */
      this.$store.commit({
        type: 'addCount',
        count
      })
    }
    /* 接收 */
        addCount(state, payload) {
        console.log(payload)
        state.counter += payload.count
    }
Vue的store中的state是响应式的,当state中的数据发生变化时,Vue组件会自动更新
这就需要我们必须遵守一些Vue对应的规则
updateInfo () {
      this.$store.commit('updateInfo', '深圳')
}
    state: {
        counter: 0,
        obj: [{
        id: 1,
        name: 'Helen',
        age: 21
      }]
    },
    mutations: {
            updateInfo(state, address) {
        // 这种数据增加,不是响应式的。
            // state.obj['address'] = address
            // 这个支持响应式,如果是数组,中间类型为number 增加属性
            Vue.set(state.obj[0], 'address', address)
            // 删除对象中的属性
            Vue.delete(state.obj[0], 'address')
        }
    }

将方法的名字 全部设置成常量保存在js文件中。
export const INCREMENT = 'increment'
export const SUB = 'sub'
export const ADDCOUNT = 'addCount'
export const UPDATEINFO = 'updateInfo'
import {
    INCREMENT,
    SUB,
    ADDCOUNT,
    UPDATEINFO
} from '../store/mutations-types'
mutations: {
        [INCREMENT](state) {
            state.counter++
        },
        [SUB](state) {
            state.counter--
        },
        // addCount(state, count) {
        //     state.counter += count
        // }
        [ADDCOUNT](state, payload) {
            console.log(payload)
            state.counter += payload.count
            state.obj.push(payload.obj)
        },
        [UPDATEINFO](state, address) {
            // 这种数据增加,不是响应式的。
            // state.obj['address'] = address
            Vue.set(state.obj[0], 'address', address)
        }
    },
一般只进行同步操作
Action类似于Mutation,但是是用来代替Mutation进行异步操作
setTimeout(() => {}, 1000) 这个函数 是异步操作。
通过dispatch到actions
HelloWorld.vue
    updateInfo () {
      // 到 mutations中
      // this.$store.commit(UPDATEINFO, '深圳')
      // 到actions中
      this.$store.dispatch('asyncUpdate', '异步操作')
    }
store/index.js
    actions: {
        /* 
          这里的context是store实例
        */
        asyncUpdate(context, content) {
            setTimeout(() => {
                // 这里提交到mutations中
                context.commit(UPDATEINFO, content)
            }, 1000)
        }
    },
Module是模块的意思
    modules: {
        a: {
            state: {},
            getters: {},
            mutations: {},
            actions: {},
            modules: {}
        }
    }