Vuex官网:https://vuex.vuejs.org/zh/
    Vuex是Vue.js应用程序的状态管理模式+库。它充当应用程序中所有组件的集中存储,其规则确保状态只能以可预测的方式进行变更。可以想象为一个“前端数据库”(数据仓库),让其在各个页面上实现数据的共享包括状态,并且可操作
    Vuex分成五个部分:

    • State:单一状态树
    • Getters:状态获取
    • Mutations:触发同步事件(触发状态改变事件)
    • Actions:提交mutation,可以包含异步操作
    • Module:将vuex进行分模块(可将vuex分为多个文件引用)

    使用方式

    1. 安装

      1. npm install vuex --save
    2. 创建store.js文件或者store模块(根据调用模块大小使用)

      1. ├─store
      2. ├─index.js
      3. ├─state.js
      4. ├─actions.js
      5. ├─mutations.js
      6. └─getters.js
    3. 在store/index.js文件中新建vuex的store实例,并注册上面引入的各大模块

    index.js(store模块)

    1. import Vue from 'vue'
    2. import Vuex from 'vuex'
    3. import state from './state'
    4. import getters from './getters'
    5. import actions from './actions'
    6. import mutations from './mutations'
    7. Vue.use(Vuex)
    8. const store = new Vuex.Store({
    9. state,
    10. getters,
    11. actions,
    12. mutations
    13. })
    14. export default store

    子文件中格式(以state为例)

    1. export default{
    2. namespaced:'state',
    3. state:{
    4. },
    5. }
    1. 在main.js中导入并使用store实例 ```javascript import store from ‘./store’

    new Vue({ store, }) ```