vuex是一个专门为vue.js应用程序开发得状态管理模式,vuex存放得状态是响应式的,更改状态只有唯一的途径显示的提交(commit)触发mutation更改state
二,由哪几部分组成 State vue的状态管理,驱动应用的数据源Mutations 同步操作 它是唯一可以直接更改state状态,通过提交commit触发mutation更改stateActions 异步操作 它不能直接改变state而是需要触发mutation来更改state,它可以任意包含异步操作Getters 计算属性 类似于vue组件中的computed,可以过滤,处理state,结果也会缓存,只有当依赖值发生改变才会从新计算Modules 模块 支持模块化 让每个模块都有自己的state,mutation,action,getter,甚至可以嵌套子模块 三,map辅助函数mapActions:
在组件内导入import { mapActions } from ‘vuex’
mapGetters import { mapGetters} from ‘vuex’
其他的mapState,mapMutations也是一样的操作
之后在store.js里面导入
import createPersistedState from 'vuex-persistedstate'export default new Vuex.Store({state,getter,mutations,actions,plugins:[createPersistedState()]})参数
storage:存储方式。(sessionStorage,localStorage)key:定义本地存储中的key