vue-vuex.md 30.7 KB
Newer Older
study夏羽's avatar
new-vue  
study夏羽 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

# 状态管理Vuex 

## 介绍


### Vuex 是什么?

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。

uni-app 内置了 [Vuex](https://vuex.vuejs.org/zh/)



### 什么是“状态管理模式”?

让我们从一个简单的 Vue 计数应用开始:


```html
<!-- view 视图-->
<template>
	<view>
		{{count}}
	</view>
</template>
<script>
	export default {
		// state 数据源
		data() {
			return {
				count: 0
			}
		},
		// actions 控制状态变化
		methods: {
			increment() {
				this.count++
			}
		}
	}
</script>
```


这个状态自管理应用包含以下几个部分:

- state,驱动应用的数据源;
- view,以声明方式将 state 映射到视图;
- actions,响应在 view 上的用户输入导致的状态变化。

以下是一个表示“单向数据流”理念的简单示意:


<img width="300px" src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b0ed6b40-45ec-11eb-bf0a-894cbc80c40a.png" />



但是,当我们的应用遇到**多个组件共享状态**时,单向数据流的简洁性很容易被破坏:

- 多个视图依赖于同一状态。
- 来自不同视图的行为需要变更同一状态。

因此,我们把组件的共享状态抽取出来,以一个全局单例模式管理。在这种模式下,我们的组件树构成了一个巨大的“视图”,不管在树的哪个位置,任何组件都能获取状态或者触发行为!这就是vuex的产生。

通过定义和隔离状态管理中的各种概念并通过强制规则维持视图和状态间的独立性,我们的代码将会变得更结构化且易维护。这就是 Vuex 背后的基本思想。


Vuex 是专门为 Vue.js 设计的状态管理库,以利用 Vue.js 的细粒度数据响应机制来进行高效的状态更新。


<img width="300px" src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b1ba7f40-45ec-11eb-8a36-ebb87efcf8c0.png" />


如果你想交互式地学习 Vuex,可以看这个 [Scrimba 上的 Vuex 课程](https://scrimba.com/learn/vuex),它将录屏和代码试验场混合在了一起,你可以随时暂停并尝试。




## 优势与使用场景

- Vuex的状态存储是响应式的,可跟踪每一个状态变化,一旦它改变,所有关联组件都会自动更新相对应的数据。
- 共享数据,解决了非父子组件的消息传递(将数据存放在state中)。
- 统一状态管理,减少了请求次数,有些情景可以直接从内存中的state获取数据。



### Vuex与全局变量区别


|vuex	|全局变量|
|--	|--	|
|不能直接改变store里面的变量,由统一的方法修改数据	|可以任意修改	|
|每个组件可以根据自己vuex的变量名引用不受影响	|全局变量可能操作命名污染	|
|解决了多组件之间通信的问题	|跨页面数据共享	|
|适用于多模块、业务关系复杂的中大型项目	|适用于demo或者小型项目	|



### 什么时候需要用vuex?

- 当一个组件需要多次派发事件时。例如购物车数量加减。
- 跨组件共享数据、跨页面共享数据。例如订单状态更新。
- 需要持久化的数据。例如登录后用户的信息。
- 当您需要开发中大型应用,适合复杂的多模块多页面的数据交互,考虑如何更好地在组件外部管理状态时。




## 项目结构


使用 Vuex 需要遵守的规则:

- 应用层级的状态应该集中到单个 `store` 对象中。

- 提交 `mutation` 是更改状态的唯一方法,并且这个过程是同步的。

- 异步逻辑都应该封装到 `action` 里面。

只要你遵守以上规则,如何组织代码随你便。如果你的 `store` 文件太大,只需将 `action``mutation``getter` 分割到单独的文件。

对于大型应用,我们会希望把 `Vuex` 相关代码分割到模块中。下面是项目结构示例:


```html
├── pages
├── static
└── store
    ├── index.js          # 我们组装模块并导出 store 的地方
    ├── actions.js        # 根级别的 action
    ├── mutations.js      # 根级别的 mutation
    └── modules           # 模块文件夹
        ├── cart.js       # 购物车模块
        └── products.js   # 产品模块
├── App.vue
├── main.js
├── manifest.json
├── pages.json
└── uni.scss
```




## 核心概念


每一个 `Vuex` 应用的核心就是 `store`(仓库),它包含着你的应用中大部分的状态 (`state`)。

状态管理有5个核心:`state``getter``mutation``action``module`



### State

单一状态树,定义应用状态的默认初始值,页面显示所需的数据从该对象中进行读取。


- `Vuex` 使用单一状态树,用一个对象就包含了全部的应用层级状态。它便作为一个“唯一数据源”而存在。这也意味着,每个应用将仅仅包含一个 store 实例。
- 单一状态树让我们能够直接地定位任一特定的状态片段,在调试的过程中也能轻易地取得整个当前应用状态的快照。
- 不可直接对 `state` 进行更改,需要通过 `Mutation` 方法来更改。


由于 `Vuex` 的状态存储是响应式的,从 `store` 实例中读取状态最简单的方法就是在计算属性中返回某个状态:

```js
// 创建一个 Counter 组件
const Counter = {
	computed: {
		count () {
			return store.state.count
		}
	}
}
```


每当 `store.state.count` 变化的时候, 都会重新求取计算属性,并且触发更新相关联的 DOM。

然而,这种模式导致组件依赖全局状态单例。在模块化的构建系统中,在每个需要使用 `state` 的组件中需要频繁地导入,并且在测试组件时需要模拟状态。



Vuex 通过 store 选项,提供了一种机制将状态从根组件“注入”到每一个子组件中(需调用 Vue.use(Vuex)):


study夏羽's avatar
study夏羽 已提交
188
1.`uni-app` 项目根目录下,新建 `store` 目录,在此目录下新建 `index.js` 文件。在 `index.js` 文件配置如下:
study夏羽's avatar
new-vue  
study夏羽 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

```js
<!-- 页面路径store/index.js -->
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);//vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state:{//存放状态
		"username":"foo"
		"age":18
	}
})
export default store
```


study夏羽's avatar
study夏羽 已提交
208
2.`main.js` 中导入文件。
study夏羽's avatar
new-vue  
study夏羽 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

```js
<!-- 页面路径main.js -->
import Vue from 'vue'
import App from './App'
import store from './store'

Vue.prototype.$store = store

// 把 store 对象提供给 “store” 选项,这可以把 store 的实例注入所有的子组件
const app = new Vue({
	store,
	...App
})
app.$mount()

```

**获取state**


study夏羽's avatar
study夏羽 已提交
230
1.通过属性访问,需要在根节点注入 `store`
study夏羽's avatar
new-vue  
study夏羽 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<text>用户名:{{username}}</text>
	</view>
</template>
<script>
	import store from '@/store/index.js';//需要引入store
	export default {
		data() {
			return {}
		},
		computed: {
			username() {
				return store.state.username 
			}
		}
	}
</script>
```

study夏羽's avatar
study夏羽 已提交
254
2.在组件中使用,通过 `this.$store` 访问到 `state` 里的数据。
study夏羽's avatar
new-vue  
study夏羽 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<text>用户名:{{username}}</text>
	</view>
</template>
<script>
	export default {
		data() {
			return {}
		},
		computed: {
			username() {
				return this.$store.state.username 
			}
		}
	}
</script>
```


#### mapState

study夏羽's avatar
study夏羽 已提交
280
3.通过 `mapState` 辅助函数获取。
study夏羽's avatar
new-vue  
study夏羽 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

当一个组件需要获取多个状态的时候,将这些状态都声明为计算属性会有些重复和冗余。
为了解决这个问题,我们可以使用 **mapState 辅助函数** 帮助我们生成计算属性,让你少按几次键:


```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>用户名:{{username}}</view>
		<view>年龄:{{age}}</view>
	</view>
</template>
<script>
	import { mapState } from 'vuex'//引入mapState
	export default {
		data() {
			return {}
		},
		computed: mapState({
		    // 从state中拿到数据 箭头函数可使代码更简练
		    username: state => state.username,
			age: state => state.age,
		}) 
	}
</script>
```


- 当映射的计算属性的名称与 `state` 的子节点名称相同时,我们也可以给 `mapState` 传一个字符串数组。

```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>用户名:{{username}}</view>
		<view>年龄:{{age}}</view>
	</view>
</template>
<script>
	import { mapState } from 'vuex'//引入mapState
	export default {
		data() {
			return {}
		},
		computed: mapState([
			'username',//映射 this.username 为 store.state.username
			'age',
		])
	}
</script>
```


- 为了能够使用 `this` 获取组件自己的data数据,必须使用常规函数。

```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>用户名:{{username}}</view>
		<view>年龄:{{age}}</view>
	</view>
</template>
<script>
	import { mapState } from 'vuex'//引入mapState
	export default {
		data() {
			return {
				firstName:"Li"
			}
		},	
		computed: {
			...mapState({
				username: function (state) {
				    return this.firstName + ' ' +  state.username 
				},
				age: state => state.age,
			})
		},
	}
</script>
```


- 使用对象展开运算符

`mapState` 函数返回的是一个对象。使用对象展开运算符将多个对象合并为一个,以使我们可以将最终对象传给 `computed` 属性。极大地简化写法:

```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>用户名:{{username}}</view>
		<view>年龄:{{age}}</view>
	</view>
</template>
<script>
	import { mapState } from 'vuex'//引入mapState
	export default {
		data() {
			return {}
		},
		computed: {
			//使用对象展开运算符将此对象混入到外部对象中
			...mapState({
				username: state => state.username,
				age: state => state.age,
			})
		},
	}
</script>
```



### Getter

可以认为是 `store` 的计算属性,对 `state` 的加工,是派生出来的数据。
- 就像 `computed` 计算属性一样,`getter` 返回的值会根据它的依赖被缓存起来,且只有当它的依赖值发生改变才会被重新计算。
- 可以在多组件中共享 `getter` 函数,这样做还可以提高运行效率。

study夏羽's avatar
study夏羽 已提交
403
`uni-app` 项目根目录下,`store` 目录 `index.js` 文件下:
study夏羽's avatar
new-vue  
study夏羽 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486

```js
<!-- 页面路径store/index.js -->
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		todos: [{
				id: 1,
				text: '我是内容一',
				done: true
			},
			{
				id: 2,
				text: '我是内容二',
				done: false
			}
		]
	},
	getters: {
		doneTodos: state => {
			return state.todos.filter(todo => todo.done)
		}
	}
})

export default store

```



`store` 上注册 `getter``getter` 方法接受以下参数:

- state, 如果在模块中定义则为模块的局部状态
- getters, 等同于 store.getters

```js
<!-- 页面路径store/index.js -->
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		todos: [{
				id: 1,
				text: '我是内容一',
				done: true
			},
			{
				id: 2,
				text: '我是内容二',
				done: false
			}
		]
	},
	getters: {
		doneTodos: state => {
			return state.todos.filter(todo => todo.done)
		},
		doneTodosCount: (state, getters) => {
			//state :可以访问数据
			//getters:访问其他函数,等同于 store.getters
			return getters.doneTodos.length
		},
		getTodoById: (state) => (id) => {
			return state.todos.find(todo => todo.id === id)
		}
	}
})

export default store
```


**获取getters**


study夏羽's avatar
study夏羽 已提交
487
1.通过属性访问,`Getter` 会暴露为 `store.getters` 对象,你可以以属性的形式访问这些值。
study夏羽's avatar
new-vue  
study夏羽 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516


```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>	
		<view v-for="(item,index) in todos">
			<view>{{item.id}}</view>
			<view>{{item.text}}</view>
			<view>{{item.done}}</view>
		</view>
	</view>
</template>
<script>
	import store from '@/store/index.js'//需要引入store
	export default {
		computed: {
			todos() {
				return store.getters.doneTodos
			}
		}
	}
</script>
```

注意,`getter` 在通过属性访问时是作为 `Vue` 的响应式系统的一部分缓存其中的。



study夏羽's avatar
study夏羽 已提交
517
2.通过 `this.$store` 访问。
study夏羽's avatar
new-vue  
study夏羽 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>	
		<view v-for="(item,index) in todos">
			<view>{{item.id}}</view>
			<view>{{item.text}}</view>
			<view>{{item.done}}</view>
		</view>
	</view>
</template>
<script>
	export default {
		computed: {
			todos() {
				return this.$store.getters.doneTodos
			}
		}
	}
</script>
```


study夏羽's avatar
study夏羽 已提交
542
3.通过方法访问。
study夏羽's avatar
new-vue  
study夏羽 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

你也可以通过让 `getter` 返回一个函数,来实现给 `getter` 传参。在你对 `store` 里的数组进行查询时非常有用。

注意,`getter` 在通过方法访问时,每次都会去进行调用,而不会缓存结果。

```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view v-for="(item,index) in todos">
			<view>{{item}}</view>
		</view>
	</view>
</template>
<script>
	export default {
		computed: {
			todos() {
				return this.$store.getters.getTodoById(2) 
			}
		}
	}
</script>
```



#### mapGetters


study夏羽's avatar
study夏羽 已提交
573
4.通过 `mapGetters` 辅助函数访问。
study夏羽's avatar
new-vue  
study夏羽 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691

`mapGetters` 辅助函数仅仅是将 `store` 中的 `getter` 映射到局部计算属性:


```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>{{doneTodosCount}}</view>
	</view>
</template>
<script>
	import {mapGetters} from 'vuex' //引入mapGetters
	export default {
		computed: {
			// 使用对象展开运算符将 getter 混入 computed 对象中
			...mapGetters([
				'doneTodos',
				'doneTodosCount',
				// ...
			])
		}
	}
</script>
```


如果你想将一个 `getter` 属性另取一个名字,使用对象形式:

```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>{{doneCount}}</view>
	</view>
</template>
<script>
	import {mapGetters} from 'vuex' //引入mapGetters
	export default {
		computed: {
			...mapGetters({
			  // 把 `this.doneCount` 映射为 `this.$store.getters.doneTodosCount`
			  doneCount: 'doneTodosCount'
			})
		}
	}
</script>
```




### Mutation


**Vuex中store数据改变的唯一方法就是mutation**


通俗的理解,`mutations` 里面装着改变数据的方法集合,处理数据逻辑的方法全部放在 `mutations` 里,使数据和视图分离。

Vuex 中的 `mutation` 非常类似于事件:每个 `mutation` 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 `state` 作为第一个参数:


```js
<!-- 页面路径store/index.js -->
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		count: 1
	},
	mutations: {
		add(state) {
			// 变更状态
			state.count += 2
		}
	}
})
export default store

```


你不能直接调用一个 mutation handler。这个选项更像是事件注册:“当触发一个类型为 add 的 `mutation` 时,调用此函数”,要唤醒一个 mutation handler,你需要以相应的 type 调用 `store.commit` 方法。


**注意**`store.commit` 调用 `mutation`(需要在根节点注入 store)。

```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>数量:{{count}}</view>
		<button @click="addCount">增加</button>
	</view>
</template>
<script>
import store from '@/store/index.js'	
export default {
	computed: {
		count() {
			return this.$store.state.count
		}
	},
	methods: {
		addCount() {
			store.commit('add')
		}
	}
}
</script>
```



study夏羽's avatar
study夏羽 已提交
692
**传入参数**
study夏羽's avatar
new-vue  
study夏羽 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800

你可以向 `store.commit` 传入额外的参数,即 `mutation` 的 载荷(payload):

还是以累加器的例子来实现 `mutation` 函数的传参,来动态定义累加的数量。


-`mutation` 传参(载荷)可以传递一个参数。

```js
<!-- 页面路径store/index.js -->
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		count: 1
	},
	mutations: {
		add(state, n) {
			state.count += n
		}
	}
})
export default store
```


```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>数量:{{count }}</view>
		<button @click="addCount">增加</button>
	</view>
</template>
<script>
	import store from '@/store/index.js'
	export default {
		computed: {
			count() {
				return this.$store.state.count
			}
		},
		methods: {
			addCount() {
				store.commit('add', 5)//每次累加 5
			}
		}
	}
</script>
```

-`mutation` 传参(载荷)可以也可以传递一个对象。让我们修改上面累加器的例子:


```js
<!-- 页面路径store/index.js -->
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		count: 1
	},
	mutations: {
		add(state, payload) {
			state.count += payload.amount
		}
	}
})
export default store

```


```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>数量:{{count }}</view>
		<button @click="addCount">增加</button>
	</view>
</template>
<script>
	import store from '@/store/index.js'
	export default {
		computed: {
			count() {
				return this.$store.state.count
			}
		},
		methods: {
			addCount () {//把载荷和type分开提交
				store.commit('add', { amount: 10 })
			}
		}
	}
</script>
```


**提交方式**


study夏羽's avatar
study夏羽 已提交
801
1.对象风格的提交方式
study夏羽's avatar
new-vue  
study夏羽 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846

我们修改组件中 `store.commit` 提交方式:

```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>数量:{{count }}</view>
		<button @click="addCount">增加</button>
	</view>
</template>
<script>
	import store from '@/store/index.js'
	export default {
		computed: {
			count() {
				return this.$store.state.count
			}
		},
		methods: {
			addCount() {//整个对象都作为载荷传给 mutation 函数
				store.commit({
					type: 'add',
					amount: 6
				})
			}
		}
	}
</script>
```


当使用对象风格的提交方式,整个对象都作为载荷传给 mutation 函数,因此 handler 保持不变:

```js
	mutations: {
		add(state, payload) {
			state.count += payload.amount
		}
	}
```


#### mapMutations

study夏羽's avatar
study夏羽 已提交
847
2.通过 `mapMutations` 辅助函数提交。
study夏羽's avatar
new-vue  
study夏羽 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900

创建组件方法提交 `mutation`

使用 `mapMutations` 辅助函数将组件中的 `methods` 映射为 `store.commit` 调用(需要在根节点注入 `store`)。


```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>数量:{{count}}</view>
		<button @click="add">增加</button>
	</view>
</template>
<script>
	import { mapMutations } from 'vuex'//引入mapMutations
	export default {
		computed: {
			count() {
				return this.$store.state.count
			}
		},
		methods: {
			...mapMutations(['add'])//对象展开运算符直接拿到add
		}
	}
</script>
```


```js
<!-- 页面路径store/index.js -->
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		count: 1
	},
	mutations: {
		add(state) {
			// 变更状态
			state.count += 2
		}
	}
})
export default store
```



study夏羽's avatar
study夏羽 已提交
901
**遵守规则**
study夏羽's avatar
new-vue  
study夏羽 已提交
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919


既然 `Vuex``store` 中的状态是响应式的,那么当我们变更状态时,监视状态的 `Vue` 组件也会自动更新。这也意味着 `Vuex` 中的 `mutation` 也需要与使用 `Vue` 一样遵守一些注意事项:

- 最好提前在你的 `store` 中初始化好所有所需属性。

- 当需要在对象上添加新属性时,你应该

	- 使用 `Vue.set(obj, 'newProp', 123)`, 或者

	- 以新对象替换老对象。例如,利用对象展开运算符我们可以这样写:

```js
	state.obj = { ...state.obj, newProp: 123 }
```



study夏羽's avatar
study夏羽 已提交
920
**Mutation 必须是同步函数**
study夏羽's avatar
new-vue  
study夏羽 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995

一条重要的原则就是要记住** mutation 必须是同步函数**

我们要通过提交 `mutation` 的方式来改变状态数据,是因为我们想要更明确地追踪到状态的变化。如果是类似下面这样异步的话:


```js
	mutations: {
		someMutation (state) {
			api.callAsyncMethod(() => {
				state.count++
			})
		}
	}
```

我们就不知道什么时候状态会发生改变,所以也就无法追踪了,这与 `mutation` 的设计初心相悖,所以强制规定它必须是同步函数。



### Action


`action` 类似于 `mutation` ,不同在于:

- action 提交的是 `mutation`,通过 `mutation` 来改变 `state` ,而不是直接变更状态。
- action 可以包含任意异步操作。


让我们来注册一个简单的 `action`

```js
<!-- 页面路径store/index.js -->
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		count: 1
	},
	mutations:{
		add(state) {
			// 变更状态
			state.count += 2
		}
	},
	actions:{
		addCountAction (context) {
		    context.commit('add')
		}
	}
})
export default store
```

`action` 函数接受一个与 `store` 实例具有相同方法和属性的 `context` 对象,因此你可以调用 `context.commit` 提交一个 `mutation`,或者通过 `context.state``context.getters` 来获取 `state``getters`


实践中,我们会经常用到 ES2015 的参数解构来简化代码(特别是我们需要调用 `commit` 很多次的时候):


```js
	actions: {
		//参数解构
		addCountAction ({commit}) {
		    commit('add')
		}
	}
```




study夏羽's avatar
study夏羽 已提交
996
**分发 Action**
study夏羽's avatar
new-vue  
study夏羽 已提交
997

study夏羽's avatar
study夏羽 已提交
998
1.`actions` 通过 `store.dispatch` 方法触发。
study夏羽's avatar
new-vue  
study夏羽 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>数量:{{count}}</view>
		<button @click="add">增加</button>
	</view>
</template>
<script>
	import store from '@/store/index.js';
	export default {
		computed: {
			count() {
				return this.$store.state.count
			}
		},
		methods: {
			add () {
				store.dispatch('addCountAction')
			}
		}
	}
</script>

```



study夏羽's avatar
study夏羽 已提交
1028
- `actions` 支持以载荷形式分发:
study夏羽's avatar
new-vue  
study夏羽 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083


```js
<!-- 页面路径store/index.js -->
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		count: 1
	},
	mutations:{
		add(state, payload) {
			state.count += payload.amount
		} 
	},
	actions:{
		addCountAction (context , payload) {
		    context.commit('add',payload)
		}
	}
})
export default store
```


```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>数量:{{count }}</view>
		<button @click="add">增加</button>
	</view>
</template>
<script>
	import store from '@/store/index.js';
	export default {
		computed: {
			count() {
				return this.$store.state.count
			}
		},
		methods: {
			add () {
				// 以载荷形式分发
				store.dispatch('addCountAction', {amount: 10})
			}
		}
	}
</script>
```


study夏羽's avatar
study夏羽 已提交
1084
- `actions` 支持以对象形式分发:
study夏羽's avatar
new-vue  
study夏羽 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161

```js
	methods: {
		add () {
			// 以对象形式分发
			store.dispatch({
				type: 'addCountAction',
				amount: 5
			})
		}
	}
```



`action` 可以执行任意的同步和异步操作

我们可以在 `action` 内部执行异步操作:


```js
<!-- 页面路径store/index.js -->
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		count: 1
	},
	mutations:{
		add(state) {
			// 变更状态
			state.count += 2
		}
	},
	actions:{
		addCountAction (context) {
			//在执行累加的时候,会等待两秒才执行
		    setTimeout(function () {
				context.commit('add')
		    }, 2000)
		}
	}
})
export default store
```


来看一个更加实际的购物车示例,涉及到**调用异步 API****分发多重 mutation**


```js
	actions: {
		checkout ({ commit, state }, products) {
			// 把当前购物车的物品备份起来
			const savedCartItems = [...state.cart.added]
			// 发出结账请求,然后乐观地清空购物车
			commit(types.CHECKOUT_REQUEST)
			// 购物 API 接受一个成功回调和一个失败回调
			shop.buyProducts(
				products,
				// 成功操作
				() => commit(types.CHECKOUT_SUCCESS),
				// 失败操作
				() => commit(types.CHECKOUT_FAILURE, savedCartItems)
			)
		}
	}
```

注意我们正在进行一系列的异步操作,并且通过提交 `mutation` 来记录 `action` 产生的状态变更。


#### mapActions 

study夏羽's avatar
study夏羽 已提交
1162
2.通过 `mapActions` 辅助函数分发。
study夏羽's avatar
new-vue  
study夏羽 已提交
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198

创建组件方法分发 `action`

- 你在组件中使用 `this.$store.dispatch('xxx')` 分发 `action`
- 或者使用 `mapActions` 辅助函数将组件的 `methods` 映射为 `store.dispatch` 调用(需要先在根节点注入 `store`


```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view>
		<view>数量:{{count }}</view>
		<button @click="addCountAction">增加</button>
	</view>
</template>
<script>
	import { mapActions } from 'vuex'
	export default {
		computed: {
			count() {
				return this.$store.state.count
			}
		},
		methods: {
			...mapActions([
			    'addCountAction', 
				// 将 `this.addCountAction()` 映射为 `this.$store.dispatch('addCountAction')`
			])
		}
	}
</script>

```



study夏羽's avatar
study夏羽 已提交
1199
- `mapActions` 也支持传入参数(载荷):
study夏羽's avatar
new-vue  
study夏羽 已提交
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210

```js
	methods: {
		...mapActions([
		    'addCountAction' 
			// 将 `this.addCountAction(amount)` 映射为 
			//`this.$store.dispatch('addCountAction', amount)`
		]),
	}
```

study夏羽's avatar
study夏羽 已提交
1211
- `mapActions` 也支持传递一个对象:
study夏羽's avatar
new-vue  
study夏羽 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222

```js
	methods: {
		...mapActions({
			addCount: 'addCountAction',
			// 将 `this.addCount()` 映射为 `this.$store.dispatch('addCountAction')`
		})
	}
```


study夏羽's avatar
study夏羽 已提交
1223
**组合 Action**
study夏羽's avatar
new-vue  
study夏羽 已提交
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294


`action` 通常是异步的,那么如何知道 `action` 什么时候结束呢?更重要的是,我们如何才能组合多个 `action`,以处理更加复杂的异步流程?

首先,你需要明白 `store.dispatch` 可以处理被触发的 `action` 的处理函数返回的 `Promise`,并且 `store.dispatch` 仍旧返回 `Promise`


```js
	actions: {
		actionA ({ commit }) {
			return new Promise((resolve, reject) => {
				setTimeout(() => {
					commit('someMutation')
					resolve()
				}, 1000)
			})
		}
	}
```

现在你可以在组件中使用:

```js
	store.dispatch('actionA').then(() => {
		// ...
	})
```


在另外一个 `action` 中也可以:


```js
	actions: {
		// ...
		actionB ({ dispatch, commit }) {
			return dispatch('actionA').then(() => {
				commit('someOtherMutation')
			})
		}
	}
```


最后,如果我们利用 `async / await`,我们可以如下组合 `action`


```js
	// 假设 getData() 和 getOtherData() 返回的是 Promise
	actions: {
		async actionA ({ commit }) {
			commit('gotData', await getData())
		},
		async actionB ({ dispatch, commit }) {
			await dispatch('actionA') // 等待 actionA 完成
			commit('gotOtherData', await getOtherData())
		}
	}
```


> 一个 `store.dispatch` 在不同模块中可以触发多个 `action` 函数。在这种情况下,只有当所有触发函数完成后,返回的 `Promise` 才会执行。



### Module

由于使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,`store` 对象就有可能变得相当臃肿。

为了解决以上问题,`Vuex` 允许我们将 `store` 分割成模块(module)。每个模块拥有自己的 `state``mutation``action``getter`、甚至是嵌套子模块——从上至下进行同样方式的分割:

study夏羽's avatar
study夏羽 已提交
1295
1.`store` 文件夹下新建 `modules` 文件夹,并在下面新建 `moduleA.js``moduleB.js` 文件用来存放 `vuex``modules` 模块。
study夏羽's avatar
new-vue  
study夏羽 已提交
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318


```html
├── components             # 组件文件夹
    └── myButton 
        └── myButton.vue   # myButton组件
├── pages
    └── index 
	    └── index.vue      # index页面
├── static
├── store
    ├── index.js          # 我们组装模块并导出 store 的地方
    └── modules           # 模块文件夹
        ├── moduleA.js    # 模块moduleA
        └── moduleB.js    # 模块moduleB
├── App.vue
├── main.js
├── manifest.json
├── pages.json
└── uni.scss
```


study夏羽's avatar
study夏羽 已提交
1319
2.`main.js` 文件中引入 `store`
study夏羽's avatar
new-vue  
study夏羽 已提交
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337

```js
<!-- 页面路径main.js -->
	import Vue from 'vue'
	import App from './App'
	import store from './store'

	Vue.prototype.$store = store

	// 把 store 对象提供给 “store” 选项,这可以把 store 的实例注入所有的子组件
	const app = new Vue({
		store,
		...App
	})
	app.$mount()
```


study夏羽's avatar
study夏羽 已提交
1338
3.在项目根目录下,新建 `store` 文件夹,并在下面新建 `index.js` 文件,作为模块入口,引入各子模块。
study夏羽's avatar
new-vue  
study夏羽 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355

```js
<!-- 页面路径store/index.js -->
	import Vue from 'vue'
	import Vuex from 'vuex'

	import moduleA from '@/store/modules/moduleA'
	import moduleB from '@/store/modules/moduleB'

	Vue.use(Vuex)
	export default new Vuex.Store({
		modules:{
			moduleA,moduleB
		}
	})
```

study夏羽's avatar
study夏羽 已提交
1356
4.子模块 `moduleA` 页面内容。
study夏羽's avatar
new-vue  
study夏羽 已提交
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375

```js
<!-- 子模块moduleA路径store/modules/moduleA.js -->
export default {
	state: {
		text:"我是moduleA模块下state.text的值"
	},
	getters: {
		
	},
	mutations: {
		
	},
	actions: { 
		
	}
}
```

study夏羽's avatar
study夏羽 已提交
1376
5.子模块 `moduleB` 页面内容。
study夏羽's avatar
new-vue  
study夏羽 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409

```js
<!-- 子模块moduleB路径store/modules/moduleB.js -->
export default {
	state: {
		timestamp: 1608820295//初始时间戳
	},
	getters: {
		timeString(state) {//时间戳转换后的时间
			var date = new Date(state.timestamp);
			var year = date.getFullYear();
			var mon  = date.getMonth()+1;
			var day  = date.getDate();
			var hours = date.getHours();
			var minu = date.getMinutes();
			var sec = date.getSeconds();
			var trMon = mon<10 ? '0'+mon : mon
			var trDay = day<10 ? '0'+day : day
			return year+'-'+trMon+'-'+trDay+" "+hours+":"+minu+":"+sec;
		}
	},
	mutations: {
		updateTime(state){//更新当前时间戳
			state.timestamp = Date.now()
		}
	},
	actions: {

	}
}
```


study夏羽's avatar
study夏羽 已提交
1410
6.在页面中引用组件 myButton ,并通过 `mapState` 读取 `state` 中的初始数据。
study夏羽's avatar
new-vue  
study夏羽 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438


```html
<!-- 页面路径:pages/index/index.vue -->
<template>
	<view class="content">
		<view>{{text}}</view>
		<view>时间戳:{{timestamp}}</view>
		<view>当前时间:{{timeString}}</view>
		<myButton></myButton>
	</view>
</template>
<script>
	import {mapState,mapGetters} from 'vuex' 
	export default {
		computed: {
			...mapState({
				text: state => state.moduleA.text,
				timestamp: state => state.moduleB.timestamp
			}),
			...mapGetters([
				'timeString'
			])
		}
	}
</script>
```

study夏羽's avatar
study夏羽 已提交
1439
7.在组件 `myButton`中,通过 `mutations` 操作刷新当前时间。
study夏羽's avatar
new-vue  
study夏羽 已提交
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465

```html
<!-- 组件路径:components/myButton/myButton.vue -->
<template>
	<view>
		<button type="default" @click="updateTime">刷新当前时间</button>
	</view>
</template>
<script>
	import {mapMutations} from 'vuex'
	export default {
		data() {
			return {}
		},
		methods: {
			...mapMutations(['updateTime'])
		}
	}
</script>

```

vue是单向数据流,子组件不能直接修改父组件的数据,而通过vuex状态管理实现:把组件的共享状态抽取出来,以一个全局单例模式管理。在这种模式下,我们的组件树构成了一个巨大的“视图”,不管在树的哪个位置,任何组件都能获取状态或者触发行为!