Vuex 和单纯的全局对象区别:

  1. Vuex 的状态存储是响应式的。当 Vue 组件从 store 中读取状态的时候,若 store 中的状态发生变化,那么相应的组件也会相应地得到高效更新。
  2. 不能直接改变 store 中的状态。改变 store 中的状态的唯一途径就是显式地提交 (commit) mutation。

State

Vuex 使用 单一状态树,用一个对象就包含了全部的应用层级状态。至此它便作为一个“唯一数据源 (SSOT)”而存在。这也意味着,每个应用将仅仅包含一个 store 实例。单一状态树让我们能够直接地定位任一特定的状态片段,在调试的过程中也能轻易地取得整个当前应用状态的快照。

获取 vuex 状态

store 实例中读取状态最简单的方法就是在计算属性中返回某个状态。

  1. computed: {
  2. count () {
  3. return store.state.count
  4. }
  5. }

避免每个使用 state 的组件都需要频繁的导入,Vuex 通过 store 选项,提供了一种机制将状态从根组件“注入”到每一个子组件中(需调用 Vue.use(Vuex)):

  1. const app = new Vue({
  2. el: #app,
  3. store
  4. })

在根实例中注册 store 选项,该 store 实例会注入到所有子组件中,子组件都能通过 this.$store 访问到。

  1. computed: {
  2. conut () {
  3. return this.$store.state.count
  4. }
  5. }

mapState

当一个组件需要获取多个状态时,可以利用 mapState 辅助函数生成计算属性。

  1. import {mapState} from 'vuex'
  2. export default {
  3. // ...
  4. computed: {
  5. // ...
  6. ...mapState({
  7. count: state => state.count,
  8. // 字符串参数等同于 state => state.count
  9. countAlias: 'count'
  10. // 如果需要获取局部状态,使用常规函数
  11. countPlusLocalState: function (state) {
  12. return state.count + this.localState
  13. }
  14. })
  15. }
  16. }

当映射的计算属性和 state 子节点相同时,可以直接传入字符串数组。

  1. computed: {
  2. // 映射 this.count 为 store.state.count
  3. ...mapState(['count'])
  4. }

Getter

从 store 中派生出一些状态,可以在 store 中定义 getter,相当于 store 中的计算属性,只有依赖值发生改变才会重新计算。

  1. const store = new Vuex.store({
  2. state: {},
  3. getters: {
  4. doneTods: state => {
  5. return this.state.todos.filter(todo => todo.done)
  6. }
  7. }
  8. })

通过属性访问

Getter 会暴露为 store.getters 对象,可以以属性的形式访问这些值。

  1. store.getters.count

Getter 也可以接受其他 getter 作为第二个参数。

  1. getters: {
  2. todosCount: (state, getters) => {
  3. return getters.todos.length
  4. }
  5. }

在组件中使用:

  1. computed: {
  2. count () {
  3. return this.$store.getters.todosCount
  4. }
  5. }

通过方法访问

让 getter 返回一个函数,实现给 getter 传参。

  1. getters: {
  2. getTodoById: state => id => {
  3. return state.todos.find(todo => todo.id === id)
  4. }
  5. }

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

mapGetters

将 getter 映射到局部计算属性。

  1. computed: {
  2. ...mapGetters(['count', 'list'])
  3. }

如果需要给 getter 属性另取名字,使用对象形式。

  1. mapGetters({
  2. count: 'doneTodosCount'
  3. })

Mutation

更改 store 中的 state 唯一方式是提交 mutation。mutation 类似事件,都已一个字符串的事件类型(type)和回调函数(handler)

  1. const store = new Vuex.store({
  2. // ...
  3. mutations: {
  4. increment(state) {
  5. state.count++
  6. }
  7. }
  8. })

用 stroe.commit 提交 mutation.

  1. store.commit('increment')

提交载荷 (Payload)

传入 store.commit 额外的参数即为 mutation 的载荷。

  1. mutations: {
  2. increment(state, payload) {
  3. state.count += payload.amount
  4. }
  5. }
  1. store.commit('increment', {
  2. amount: 10
  3. })

对象风格提交

提交 mutation 的另一种方式。

  1. store.commit({
  2. type: 'increment',
  3. amount: 10
  4. })

Mutation 需遵循 Vue 的响应规则

Vuex 的 store 中的状态是响应式的,那么当我们变更状态时,监视状态的 Vue 组件也会自动更新。

  1. 最好提前在你的 store 中初始化好所有所需属性。
  2. 当需要在对象上添加新属性时,你应该
  • 使用 Vue.set(obj, 'newProp', 123), 或者
  • 以新对象替换老对象。例如,利用对象展开运算符我们可以这样写:
  1. state.obj = { ...state.obj, newProp: 123 }

使用常量代替事件类型

使用常量替代 mutation 事件类型在各种 Flux 实现中是很常见的模式。这样可以使 linter 之类的工具发挥作用,同时把这些常量放在单独的文件中可以让你的代码合作者对整个 app 包含的 mutation 一目了然。

Mutation 必须是同步函数

异步函数执行让状态的改变都是不可追踪。

mapMutations

在组件中可以使用 this.$store.commit() 提交 mutation,也可以使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(需要在根节点注入 store)

  1. import {mapMutations} from 'vuex'
  2. export default {
  3. // ...
  4. methods: {
  5. ...mapMutaions([
  6. 'increment', // this.increment() 映射到 this.$store.commit('increment')
  7. // mapMutations 也支持载荷
  8. 'incrementBy' // this.incrementBy() 映射到 this.$store.commit('incrementBy')
  9. ]),
  10. ...mapMutations({
  11. add: 'increment' // this.add() 映射到 this.$.increment.commit('increment')
  12. })
  13. }
  14. }

Acitin

Action 提交的是 mutation,而不是直接变更状态。可以包含任意异步操作。

  1. const store = new Vuex.Store({
  2. state: {},
  3. mutations: {},
  4. actions: {
  5. increment(context) {
  6. context.commit('increment')
  7. }
  8. }
  9. })

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

可以使用结构赋值简写:

  1. const store = new Vuex.Store({
  2. // ...
  3. actions: {
  4. increment({commit, state, getters}, data) {
  5. commit('increment', data)
  6. }
  7. }
  8. })

分发 Action

  1. store.dispath('increment')

Actions 支持同样的载荷方式和对象方式进行分发:

  1. // 载荷方式
  2. store.dispath('increment', {
  3. amount: 10
  4. })
  5. // 对象方式
  6. store.dispath({
  7. type: 'increment',
  8. amount: 10
  9. })

在组件中分发 Action

使用 this.$store.dispath('increment')。或者使用 mapActions。

  1. methods: {
  2. ...mapActions([
  3. 'increment'
  4. ]),
  5. ...mapActions({
  6. add: 'increment'
  7. })
  8. }

组合 Action

store.dispatch 可以处理被触发的 action 的处理函数返回的 Promise,并且 store.dispatch 仍旧返回 Promise:

  1. acitons: {
  2. 'actions1'({commit}) {
  3. return new Promise((resolve, reject) => {
  4. setTimeout(() => {
  5. commit('aMutation')
  6. resolve()
  7. }, 1000)
  8. })
  9. }
  10. }
  1. store.dispatch('actions1').then(() => {})

Module

应用的所用状态集中在一个比较大的对象里,sotre 会变得臃肿。

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

  1. const moduleA = {}
  2. const moduleB = {}
  3. const store = new Vuex.Store({
  4. modules: {
  5. a: moduleA,
  6. b: moduleB
  7. }
  8. })
  9. store.state.a // -> moduleA 的状态
  10. store.state.b // -> moduleB 的状态

局部状态

对于模块内部的 mutation 和 getter,接收的第一个参数是模块的局部状态对象,getter 的的三个参数是根节点状态。

  1. const moduleA = {
  2. state: { count: 0 },
  3. mutations: {
  4. increment (state) {
  5. // 这里的 `state` 对象是模块的局部状态
  6. state.count++
  7. }
  8. },
  9. getters: {
  10. doubleCount (state, getters, rootState) {
  11. return state.count * 2
  12. }
  13. }
  14. }

对于模块内部的 action,局部状态通过 context.state 暴露出来,根节点状态则为 context.rootState:

  1. const moduleA = {
  2. // ...
  3. actions: {
  4. incrementIfOddOnRootSum ({ state, commit, rootState }) {
  5. if ((state.count + rootState.count) % 2 === 1) {
  6. commit('increment')
  7. }
  8. }
  9. }
  10. }

插件

logger 插件,修改state 时,会在控制台打印日志。

  1. import createLogger from 'vuex/dist/logger'
  2. Vue.use(Vuex)
  3. // 调试工具,在开发环境下使用
  4. const debug = process.env.NODE_ENV !== 'production'
  5. export default new Vuex.Store({
  6. actions,
  7. getters,
  8. state,
  9. mutations,
  10. // 严格模式,开启会检测 state 的修改是否来源于 mutation
  11. strict: debug,
  12. plugins: debug ? [createLogger()] : []
  13. })