Vuex 的核心概念:State, Mutations, Actions, Getters

Vuex 的核心概念:State, Mutations, Actions, Getters

Vuex 是 Vue.js 的官方状态管理库,提供了集中式的状态管理机制。它的核心概念包括 State(状态)、Mutations(变更)、Actions(动作)和 Getters(获取器)。以下是对这些概念的详细介绍及相应的代码示例。

State(状态)

State 是存储应用共享状态的地方。在 Vuex 中,State 是一个对象,包含应用中需要共享的所有数据。组件可以从 store 中读取状态,当状态发生变化时,依赖这些状态的组件会自动更新。

示例:

javascript 复制代码
// store.js
import { createStore } from 'vuex';

const store = createStore({
  state() {
    return {
      count: 0,
      todos: [
        { id: 1, text: 'Learn Vuex', done: true },
        { id: 2, text: 'Build a project', done: false },
      ],
    };
  },
});

export default store;

在组件中访问 state:

javascript 复制代码
// MyComponent.vue
<template>
  <div>
    <p>Count: {{ count }}</p>
    <ul>
      <li v-for="todo in todos" :key="todo.id">
        {{ todo.text }} - {{ todo.done ? 'Done' : 'Pending' }}
      </li>
    </ul>
  </div>
</template>

<script>
import { mapState } from 'vuex';

export default {
  computed: {
    ...mapState(['count', 'todos']),
  },
};
</script>

Mutations(变更)

Mutations 是更改 Vuex 中 state 的唯一方法。每个 mutation 都有一个字符串的事件类型(type)和一个回调函数(handler)。回调函数接收 state 作为第一个参数,可以直接修改 state。需要注意的是,mutations 必须是同步函数。

示例:

javascript 复制代码
// store.js
const store = createStore({
  state() {
    return {
      count: 0,
    };
  },
  mutations: {
    increment(state) {
      state.count++;
    },
    decrement(state) {
      state.count--;
    },
  },
});

在组件中提交 mutation:

javascript 复制代码
// MyComponent.vue
<template>
  <div>
    <p>Count: {{ count }}</p>
    <button @click="increment">Increment</button>
    <button @click="decrement">Decrement</button>
  </div>
</template>

<script>
import { mapState } from 'vuex';

export default {
  computed: {
    ...mapState(['count']),
  },
  methods: {
    increment() {
      this.$store.commit('increment');
    },
    decrement() {
      this.$store.commit('decrement');
    },
  },
};
</script>

Actions(动作)

Actions 类似于 mutations,不同之处在于:

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

示例:

javascript 复制代码
// store.js
const store = createStore({
  state() {
    return {
      count: 0,
    };
  },
  mutations: {
    increment(state) {
      state.count++;
    },
  },
  actions: {
    incrementAsync({ commit }) {
      setTimeout(() => {
        commit('increment');
      }, 1000);
    },
  },
});

在组件中分发 action:

javascript 复制代码
// MyComponent.vue
<template>
  <div>
    <p>Count: {{ count }}</p>
    <button @click="incrementAsync">Increment Async</button>
  </div>
</template>

<script>
import { mapState } from 'vuex';

export default {
  computed: {
    ...mapState(['count']),
  },
  methods: {
    incrementAsync() {
      this.$store.dispatch('incrementAsync');
    },
  },
};
</script>

在上述示例中,incrementAsync action 等待 1 秒后提交 increment mutation,从而异步地增加计数器的值。

Getters(获取器)

Getters 类似于组件的计算属性,用于从 state 中派生出一些状态。它们可以对 state 进行处理并返回结果,且结果会被缓存,只有当相关的 state 发生变化时才会重新计算。

示例:

javascript 复制代码
// store.js
const store = createStore({
  state() {
    return {
      todos: [
        { id: 1, text: 'Learn Vuex', done: true },
        { id: 2, text: 'Build a project', done: false },
      ],
    };
  },
  getters: {
    doneTodos: (state) => {
      return state.todos.filter((todo) => todo.done);
    },
    doneTodosCount: (state, getters) => {
      return getters.doneTodos.length;
    },
  },
});

在组件中访问 getters:

javascript 复制代码
// MyComponent.vue
<template>
  <div>
    <p>Done Todos Count: {{ doneTodosCount }}</p>
    <ul>
      <li v-for="todo in doneTodos" :key="todo.id">
        {{ todo.text }}
      </li>
    </ul>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';

export default {
  computed: {
    ...mapGetters(['doneTodos', 'doneTodosCount']),
  },
};
</script>

在上述示例中,doneTodos getter 返回已完成的待办事项列表,doneTodosCount getter 返回已完成的待办事项数量。

通过合理地使用 Vuex 的这些核心概念,可以使 Vue.js 应用的状态管理更加清晰、可维护和可预测。

相关推荐
teeeeeeemo1 小时前
Vue数据响应式原理解析
前端·javascript·vue.js·笔记·前端框架·vue
Sahas10191 小时前
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ is not explicitly defined.
前端·javascript·vue.js
Jinxiansen02111 小时前
Vue 3 实战:【加强版】公司通知推送(WebSocket + token 校验 + 心跳机制)
前端·javascript·vue.js·websocket·typescript
我在北京coding2 小时前
Uncaught ReferenceError: process is not defined
前端·javascript·vue.js
baozj2 小时前
一次表单数据复用引发的 Bug:理解 Vue 中的 data 为何是函数
前端·javascript·vue.js
Nano3 小时前
ES6中的Proxy和Reflect:深入解析与Vue3响应式原理的完美结合
前端·vue.js
前端工作日常3 小时前
Vue源码中为何使用new Function而不使用eval?
vue.js
工业互联网专业3 小时前
基于django+vue的健身房管理系统-vue
vue.js·python·django·毕业设计·源码·课程设计·健身房管理系统
jstart千语3 小时前
【vue3学习】vue3入门
前端·javascript·vue.js·typescript·vue
一个儒雅随和的男子3 小时前
Vue中虚拟DOM的原理与作用
前端·javascript·vue.js