htmledit_views">
Vuex作用
集中管理项目公共数据
为什么不直接创建一个全局变量来代替Vuex?
- Vuex 是响应式的
- Vuex 可以跟踪每一个状态的变化
基本使用方法
- 创建state
- 获取state 中数据的方式:
- {{ $store.state.count }}
- 在computed 中定义方法,返回 this.$store.state.count
- 批量获取state 中的数据
- 修改state 数据 mutation 和commit
- 在mutation 中 定义方法:this.$store.commit('方法名称',参数) 不要写异步,否则不可追踪
- getters:类似语computed
- action: 定义调用mutation 中方法中的方法,异步方法