0)store是仓库,响应式的,state变化,对应组件也会更新,
1)存数据
在某页面中,定义数组datalist【】
在created中同步的方式存值:this.$store.commit(方法名',值)
this.$store.commit('changeValue',this.datalist)
2)在store中定义方法和数组
在state中定义变量名:datalist:[]
在mutations中定义同步方法名'changeValue'(state,newVal){
State. Datalist= newVal
3)拿数据
——3.1)正常获取数据
State中的数据——this.$store.state.datalist
模块中的数据——this.$store.state.模块. Datalist
——3.2)在computed中读取数据
Computed:{
Count(){
Return store.state.count
}
}
在dom中直接使用{{count}}
——3.3)辅助函数获取数据
先引入辅助函数import { mapGetters, mapState } from 'vuex';
再使用载computed中,
...mapState({
classIntroduceArr: (state) => state.dataContainer.classIntroduceArr
量名:从state/模块/变量名
4)拿方法
拿方法——this.$store.diapatch.(“模块/方法”)
拿方法——this.$store.commit.(“模块/方法”)¬——同步触发写在mutations
拿方法——this.$store.commit.(“模块/方法”)¬——异步——触发写在actions