定义:vue全局状态管理器 定义(在任意组件/页面都可以访问vuex的数据)(当数据更新的时候,引用vuex的组件视图会自动更新)
一.vuex的五大核心
1.state(定义数据,相当于组件中的data),state中的数据可以在任何组件中进行调用,组建中访问state中的数据用$store.state.数组名.数据属性
2.getters(计算数据,相当于组建中computed),组件中访问$store.getters.方法名
3.mutations(唯一可以修改state中的数据),其中参数state参数是必须的,也可以自己传递一个参数,如下代码,
组件中访问$store.commit("方法名",参数)
4.actions(异步操作),组件中访问$store.dispatch("方法名",参数)
5.modules
modules模块
模块文件夹中的模块首先要在index.js中modules中引入,例如:
1. 模块中的state:组件中访问模块的state需要带模块名称(cart)$store.state.cart.goods
2.组件访问getters,actions,mutations不需要带模块名$store.getters.方法名 组件访问getter,操作,突变,不需要带模块名$store.getters.方法名
二.vuex的映射
1.使用映首先要在使用的页面导入:
import {mapstate,mapGetters,mapActions,mapMutations}from 'vuex' 从“vuex”
2.mapGetters,mapMutations需要在methods中扩展,例如:
3.mapstate,mapActions要在computed中扩展