Vue2_VueX
Vuex 简介
什么是 vuex
概念:专门在 Vue 中实现集中式状态(数据)管理的一个 Vue 插件,对 vue 应用中多个组件的共享状态进行集中式的管理(读/写),也是一种组件间通信的方式,且适用于任意组件间通信。
什么时候使用 vuex:
- 01 多个组件依赖于同一状态
- 02 来自不同组件的行为需要变更同—状态
纯 vue 实现计数器案例
App.vue:
1 | <template> |
Count.vue:
1 | <template> |
Vuex 工作原理图
vuex 的使用
- 01 安装 vuex
1 | npm install vuex@3 |
- 02 引入 vuex 并使用 vuex 插件,并配置数据仓库
./src/store/index.js:
1 | //该文件用来创建Vuex中最为核心的store |
- 03 在 main.js 中导入并注册 store 数据仓库即可
1 | import Vue from 'vue' |
vuex 实现计数器案例
- 01 在按钮的点击事件回调中派发 actions 或提交 mutations
- 派发 actions 时,参数一要与 actions 中的方法名一致
- 直接提交 mutations 时,参数一要与 mutations 中的方法名一致,通常为全部大写,
- 通过 mutations 修改 state,若在 actions 中操作 state,则开发者工具就派不上用场了
- 04 在组件模板中可以通过 $store.state.sum ,JS 中通过 this.$store.state.sum 获取到数据仓库 state 中的数据
Count.vue:
1 | <template> |
02 在数据仓库的 actions 中,进行其他业务处理,并提交 mutations
- 提交 mutations 时,参数一通常为该方法名的全部大写,与 mutations 中的方法名一致
03 在 mutations 中操作 state 中的数据
./src/store/index.js:
1 | //该文件用来创建Vuex中最为核心的store |
getters 对 state 的操作
- 01 在以上案例的基础上,添加 getters 配置项,对 state 进行加工/简化操作
./src/store/index.js:
1 | //准备getters 对state数据的加工简化操作,相当于计算属性 |
- 02 在组件模板 [或 JS ] 中通过 [this.]$store.getters.bigSum 获取到经过 getters 加工过的数据
Count.vue:
1 | <h1>当前求和放大10倍:{{ $store.getters.bigSum }}</h1> |
mapState
- 01 在数据仓库中准备数据
./src/store/index.js:
1 | //准备state |
- 02 在组件中获取数据,从 vuex 中导入 mapState
- 03 在组件的计算属性中,进行简化操作
- 04 在组件模块中使用数据
Count.vue:
1 | import { mapState } from 'vuex' |
1 | computed:{ |
1 | <!-- <h1>学校:{{ school }},,,学科:{{ xueke }}</h1> --> |
mapGetters
- 01 在数据仓库的 getters 中对数据进行加工简化操作
./src/store/index.js:
1 | //准备getters 对state数据的加工简化操作,相当于计算属性 |
02 在组件中获取数据,,从 vuex 中导入 mapGetters
03 在组件的计算属性中,进行简化操作
04 在组件模块中使用数据
Count.vue:
1 | import { mapState, mapGetters } from 'vuex' |
1 | computed:{ |
1 | <h1>当前求和放大10倍:{{ bigSum }}</h1> |
mapMutation
- 01 在数据仓库的 mutations 中:
./src/store/index.js:
1 | //准备mutations |
02 在组件中获取数据,,从 vuex 中导入 mapMutations
03 在组件的 methods 方法中,进行简化操作
04 在组件模块中使用方法
Count.vue:
1 | import { mapState, mapGetters, mapMutations, mapActions } from 'vuex' |
1 | methods: { |
1 | <button @click="increment(n)">+</button> <button @click="decrement(n)">-</button> |
mapActions
- 01 在数据仓库的 actions 中:
./src/store/index.js:
1 | //准备actions |
02 在组件中获取数据,,从 vuex 中导入 mapActions
03 在组件的 methods 方法中,进行简化操作
04 在组件模块中使用方法
Count.vue:
1 | import { mapState, mapGetters, mapMutations, mapActions } from 'vuex' |
1 | methods: { |
1 | <button @click="incrementOdd(n)">当前求和为奇数再加</button> <button @click="incrementWait(n)">等一等再加</button> |
多组件共享数据
- 01 在数据仓库中准备数据
./src/store/index.js:
1 | //准备state |
- 02 在 Person.vue 组件中创造一个数据,并添加到数据仓库中
- 03 在 Person.vue 组件中获取数据仓库中的 sum 数据,并展示在页面中
1 | <template> |
- 04 在 Count.vue 组件中获取到数据仓库中的 personList 数据,并展示在页面中
1 | import { mapState, mapGetters, mapMutations, mapActions } from 'vuex' |
1 | computed: { |
1 | <h1>下方列表的总人数是:{{personList.length}}</h1> |
vuex 模块化与命名空间 1
- 01 在数据仓库中,将各项模块化,并开启命名空间
./src/store/index.js:
1 | //该文件用来创建Vuex中最为核心的store |
- 02 在组件中获取数据仓库中的数据
Count.vue:
1 | <template> |
Person.vue
1 | <template> |
vuex 模块化与命名空间 2
- 01 将数据仓库划分多个小仓库
./src/store/index.js:
1 | //该文件用来创建Vuex中最为核心的store |
./src/store/countOptions.js:
1 | import axios from 'axios' |
./src/store/personOptions.js:
1 | export default { |
- 02 在组件中获取数据仓库中的数据
Person.vue:
1 | <template> |
Count.vue:
1 | <template> |
- 感谢你赐予我前进的力量