状态管理 Vuex

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

什么是状态管理

在 Vue 应用中,组件之间需要共享数据:

  • 父子组件通过 props 和 events 通信
  • 兄弟组件需要通过父组件中转
  • 跨层级组件通信更加复杂

当应用变得复杂时,这种分散的状态管理方式变得难以维护。Vuex 提供了一个统一的状态管理中心,解决了这些问题。

核心概念

State

单一状态树,存储应用的所有状态:

const store = new Vuex.Store({
  state: {
    count: 0,
    user: null,
    todos: []
  }
})

Getter

类似计算属性,对 state 进行派生:

const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, text: '学习 Vue', done: true },
      { id: 2, text: '学习 Vuex', done: false }
    ]
  },
  getters: {
    doneTodos: state => {
      return state.todos.filter(todo => todo.done)
    },
    doneTodosCount: (state, getters) => {
      return getters.doneTodos.length
    }
  }
})

Mutation

同步更改状态:

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    },
    incrementBy(state, payload) {
      state.count += payload.amount
    }
  }
})

Action

异步操作,提交 mutation:

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    }
  },
  actions: {
    incrementAsync({ commit }) {
      setTimeout(() => {
        commit('increment')
      }, 1000)
    }
  }
})

Module

模块化组织代码:

const moduleA = {
  state: { ... },
  mutations: { ... },
  actions: { ... },
  getters: { ... }
}

const store = new Vuex.Store({
  modules: {
    a: moduleA
  }
})

安装 Vuex

npm install vuex@3

基本使用

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    }
  },
  actions: {
    increment({ commit }) {
      commit('increment')
    }
  },
  getters: {
    doubleCount: state => state.count * 2
  }
})

new Vue({
  store,
  render: h => h(App)
}).$mount('#app')

在组件中使用

export default {
  computed: {
    count() {
      return this.$store.state.count
    },
    doubleCount() {
      return this.$store.getters.doubleCount
    }
  },
  methods: {
    increment() {
      this.$store.commit('increment')
    },
    incrementAsync() {
      this.$store.dispatch('increment')
    }
  }
}

为什么需要 Vuex

统一状态管理

所有状态集中在一个地方,便于追踪和调试:

┌─────────────────────────────────────┐
│             Vuex Store              │
│  ┌─────────────────────────────┐    │
│  │           State             │    │
│  │  - user                     │    │
│  │  - products                 │    │
│  │  - cart                     │    │
│  └─────────────────────────────┘    │
│                 ↑                   │
│  ┌──────────────┼──────────────┐    │
│  │              │              │    │
│  ↓              ↓              ↓    │
│Component A  Component B  Component C│
└─────────────────────────────────────┘

可预测的状态变更

通过 mutation 修改状态,每次变更都有记录:

store.commit('setUser', user)

时间旅行调试

配合 Vue Devtools,可以回退到任意状态:

mutation: setUser
mutation: addToCart
mutation: removeFromCart
← 回退到之前的状态

何时使用 Vuex

适合使用 Vuex 的场景:

  • 中大型单页应用
  • 多个组件共享状态
  • 需要跨组件通信
  • 需要状态持久化
  • 需要状态历史记录

不需要 Vuex 的场景:

  • 小型应用
  • 组件间无共享状态
  • 简单的父子通信

本章内容

  • Vuex 简介:核心概念和基本使用
  • State 状态:状态定义和访问
  • Getter 派生:派生状态和计算属性
  • Mutation 同步更新:同步修改状态
  • Action 异步操作:异步操作和 API 调用
  • Module 模块化:模块组织大型应用

学习建议

学习 Vuex 的步骤:

  1. 理解核心概念:State、Getter、Mutation、Action
  2. 掌握基本使用:在组件中访问和修改状态
  3. 学习辅助函数:mapState、mapGetters、mapMutations、mapActions
  4. 实践模块化:拆分大型应用状态
  5. 配合 Devtools:使用调试工具提高效率