Skip to content

Vuex

Vuex 是 Vue.js 的状态管理库,用于管理 Vue 应用中的状态(state)。它提供了一种集中式存储管理的方式,使得多个组件可以共享和响应应用的状态变化。Vuex 的设计灵感来自于 Flux、Redux 等状态管理的模式。

Vuex 的核心概念:

  1. State(状态):

    State 即应用中需要共享的数据。在 Vuex 中,所有的共享数据都被集中存储在一个单一的状态树(state tree)中。

    javascript
    // 示例
    const store = new Vuex.Store({
      state: {
        count: 0
      }
    });
  2. Getter(获取器):

    Getter 允许你在获取 state 中的数据时进行一些计算或处理。Getter 可以看作是 store 中的计算属性。

    javascript
    // 示例
    const store = new Vuex.Store({
      state: {
        count: 0
      },
      getters: {
        doubleCount: state => state.count * 2
      }
    });
    
    // 在组件中使用
    computed: {
      doubleCount() {
        return this.$store.getters.doubleCount;
      }
    }
  3. Mutation(突变):

    Mutation 是唯一允许修改 state 的途径。每个 Mutation 都是一个纯函数,接收当前的 state 和一个 payload,用来修改 state。

    javascript
    // 示例
    const store = new Vuex.Store({
      state: {
        count: 0
      },
      mutations: {
        increment(state) {
          state.count++;
        },
        decrement(state) {
          state.count--;
        }
      }
    });
    
    // 在组件中使用
    methods: {
      increment() {
        this.$store.commit('increment');
      },
      decrement() {
        this.$store.commit('decrement');
      }
    }
  4. Action(动作):

    Action 用于处理异步操作或复杂逻辑,并提交 Mutation 来修改 state。Action 类似于 Mutation,但 Action 提交的是 Mutation 而不是直接修改 state。

    javascript
    // 示例
    const store = new Vuex.Store({
      state: {
        count: 0
      },
      mutations: {
        increment(state) {
          state.count++;
        }
      },
      actions: {
        asyncIncrement(context) {
          setTimeout(() => {
            context.commit('increment');
          }, 1000);
        }
      }
    });
    
    // 在组件中使用
    methods: {
      asyncIncrement() {
        this.$store.dispatch('asyncIncrement');
      }
    }
  5. Module(模块):

    Module 允许你将 store 分割成模块,每个模块都有自己的 state、getter、mutation 和 action。这使得大型应用的状态管理更加容易维护。

    javascript
    // 示例
    const moduleA = {
      state: { /* ... */ },
      mutations: { /* ... */ },
      actions: { /* ... */ }
    };
    
    const moduleB = {
      state: { /* ... */ },
      mutations: { /* ... */ },
      actions: { /* ... */ }
    };
    
    const store = new Vuex.Store({
      modules: {
        a: moduleA,
        b: moduleB
      }
    });

以上是 Vuex 的核心概念,通过这些概念,你可以在 Vue 应用中更好地管理和组织应用的状态。 Vuex 提供了一种清晰的数据流和一致的状态管理模式,适用于中大型应用的复杂状态管理场景。