VueX【专门在vue中实现集中式状态或数据管理的vue插件】
1.多个组建依赖同一个状态
2.来自不同组件的行为需要变更同一个状态
使用:
1.下载npm i vuex@2导入(import)使用(use)
2.配置actions,mutations,state,导出store
3.vc中this.
s
t
o
r
e
.
d
i
s
p
a
t
c
h
(
′
操
作
′
,
d
a
t
e
)
【
t
h
i
s
.
store.dispatch('操作',date) 【this.
store.dispatch(′操作′,date)【this.store.commit(‘操作’,date) 】
4.action{操作(context,value){context.commit(‘操作’,value)}}【context.dispatch(‘操作’,value)】
5.mutations{操作(state,value){state.xx+=date}}
6.$store.state.xx
getters:【对state数据进一步处理,类似于computed】{xx(state){return}}
map优化:
1.用于映射state/getters中的数据为计算属性computed
。。。mapstate({key:【state属性】})
。。。mapstate(【'state属性'】)
。。。mapGetters()【同上】
2.用于生成与action/mutations对话的方法methods【传递的数据在@事件绑定时添加,不添加传递event】
。。。mapActions({绑定函数:'操作',})
。。。mapActions(【'操作'】)
。。。mapMutations({绑定函数:'操作',})
。。。mapMutations(【'操作'】)
模块化+命名空间:namespace:true//开启命名空间 【将不同配置写入不同js文件,在在index引入】
。。。mapstate(‘countAbout’,[‘xx’,‘xx’])【从countAbout中state读取数据】
。。。mapGetters(‘countAbout’,[‘xx’,‘xx’])
。。。mapActions(‘countAbout’,[‘xx’,‘xx’])
。。。mapMutations(‘countAbout’,[‘xx’,‘xx’])