蓝天白云蓝天白云
2023-03-02
vue
103

Vuex

Vuex 是一个状态管理库,用于在 Vue.js 应用程序中管理数据的状态。它提供了统一的状态管理机制,让我们可以更好地组织和管理组件之间共享的数据,并方便地实现复杂的应用逻辑。本篇博客将带领读者入门 Vuex,并介绍其核心概念和用法。

为什么需要 Vuex

在 Vue.js 应用程序中,组件间的通信是通过 prop 和事件进行的。对于简单的应用,这种方式很容易理解和使用。但是对于大型的复杂应用,组件之间的数据共享和状态管理变得非常困难。例如,如果一个组件需要更新另一个组件的数据,就需要通过事件或 prop 进行传递,而这种通信方式会导致代码复杂度增加,不易调试和维护。

如果我们使用 Vuex,可以将应用程序的状态集中管理,让所有组件都能够访问和修改同一份数据。这样,我们就可以方便地实现组件之间的数据共享和状态管理,提高代码的可维护性和可扩展性。

基本概念

在理解 Vuex 的用法之前,我们先来了解一下它的基本概念。

State

State 表示应用程序的状态,包含多个属性,每个属性对应着一个数据项。我们可以通过 Vuex.Store 构造函数的 options.state 选项定义应用程序的状态:

const store = new Vuex.Store({
 state: {
   count: 0
}
})

上面的代码定义了一个名为 count 的状态属性,初始值为 0。

Mutation

Mutation 表示对 State 进行更改的方法,每个 Mutation 都有一个字符串类型的事件类型 (type) 和一个回调函数 (handler)。我们可以通过 Vuex.Store 构造函数的 options.mutations 选项定义 Mutation:

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

上面的代码定义了一个名为 increment 的 Mutation,当调用该 Mutation 时,会将 State 中的 count 属性值加 1。

Action

Action 表示提交 Mutation 的方法,并可以异步地执行一些操作。每个 Action 都有一个字符串类型的事件类型 (type) 和一个回调函数 (handler)。我们可以通过 Vuex.Store 构造函数的 options.actions 选项定义 Action:

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

上面的代码定义了一个名为 incrementAsync 的 Action,该 Action 在延迟 1 秒后提交一个名为 increment 的 Mutation。

Getter

Getter 表示从 State 中获取数据的方法,类似于计算属性。每个 Getter 都有一个字符串类型的名称 (name) 和一个回调函数 (handler)。我们可以通过 Vuex.Store 构造函数的 options.getters 选项定义 Getter:

const store = new Vuex.Store({
 state: {
   count: 0
},
 mutations: {
   increment(state) {
     state.count++
  }
},
 getters: {
   count(state) {
     return state.count
  }
}
})

上面的代码定义了一个名为 count 的 Getter,用于从 State 中获取 count 属性的值。

Modules

当 Vuex 应用变得复杂时,我们可以将 Store 拆分成多个模块,每个模块都拥有自己的 State、Mutation、Action 和 Getter,这样可以更好地组织和管理代码。

在创建 Store 实例时,可以使用 options.modules 选项来定义多个模块:

const moduleA = {
 state: { ... },
 mutations: { ... },
 actions: { ... },
 getters: { ... }
}
​
const moduleB = {
 state: { ... },
 mutations: { ... },
 actions: { ... },
 getters: { ... }
}
​
const store = new Vuex.Store({
 modules: {
   a: moduleA,
   b: moduleB
}
})

上面的代码中,我们定义了两个模块 moduleA 和 moduleB,并将它们作为 a 和 b 属性添加到 Store 实例的 modules 选项中

基本用法

了解了 Vuex 的基本概念之后,我们来看一下它的基本用法。首先,我们需要在应用程序中安装 Vuex 并创建 Store 实例:

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: {
   incrementAsync(context) {
     setTimeout(() => {
       context.commit('increment')
    }, 1000)
  }
},
 getters: {
   count(state) {
     return state.count
  }
}
})

上面的代码中,我们使用 import 导入了 Vuex,并通过 Vue.use(Vuex) 将其安装到 Vue 上。然后,我们创建了一个名为 store 的 Vuex.Store 实例,并定义了基本的 State、Mutation、Action 和 Getter。

在组件中使用 Vuex 需要使用 Vue.mixin 方法将 Store 实例注入到组件实例中:

import Vue from 'vue'
​
Vue.mixin({
 beforeCreate() {
   const options = this.$options
   if (options.store) {
     this.$store = options.store
  } else if (options.parent && options.parent.$store) {
     this.$store = options.parent.$store
  }
}
})

上面的代码中,我们使用 Vue.mixin 方法扩展了 Vue 实例的 beforeCreate 钩子函数。在钩子函数中,我们通过 this.$options 可以获取 Vue 实例的 options 选项,通过判断选项中是否包含 store 属性,即可将 Store 实例注入到该组件实例的 $store 属性中。

现在,我们已经将 Store 实例注入到组件实例中了,接下来就可以在组件中使用 Vuex 提供的各种 API 了。

  1. 获取 State

在组件中使用 $store.state 可以访问应用程序的 State,例如:

<template>
 <div>
   <p>{{ $store.state.count }}p>
   <button @click="$store.commit('increment')">Incrementbutton>
   <button @click="$store.dispatch('incrementAsync')">Increment Asyncbutton>
 div>
template>

上面的代码中,我们通过 $store.state.count 获取 State 中的 count 属性,并通过 $store.commit('increment') 调用名为 increment 的 Mutation。同时,我们还通过 $store.dispatch('incrementAsync') 调用名为 incrementAsync 的 Action。

  1. 使用 Getter

在组件中使用 $store.getters 可以访问应用程序的 Getter,例如:

<template>
 <div>
   <p>{{ $store.getters.count }}p>
 div>
template>

上面的代码中,我们通过 $store.getters.count 获取名为 count 的 Getter 的返回值。

  1. 使用Modules

在组件中访问 State、Mutation、Action 和 Getter 时需要加上模块名称前缀,例如:

复制代码<template>
 <div>
   <p>{{ $store.state.a.count }}p>
   <button @click="$store.commit('a/increment')">Incrementbutton>
   <button @click="$store.dispatch('a/incrementAsync')">Increment Asyncbutton>
   <p>{{ $store.getters['b/count'] }}p>
 div>
template>

上面的代码中,我们通过 $store.state.a.count 访问模块 a 中的 count 属性,并通过 $store.commit('a/increment') 调用模块 a 中的 increment Mutation。同时,我们还通过 $store.dispatch('a/incrementAsync') 调用模块 a 中的 incrementAsync Action。另外,由于模块 b 没有定义名为 count 的 Getter,因此我们通过 $store.getters['b/count'] 来访问模块 b 中的 count Getter。

总结

Vuex 是一个强大的状态管理库,可以帮助我们更好地组织和管理 Vue.js 应用程序中的数据和状态。在使用 Vuex 时,需要理解其基本概念和用法,尤其是 State、Mutation、Action 和 Getter 这几个核心概念。同时,还需要注入 Store 实例到组件中,并在组件中使用 Vuex 提供的 API 来访问 State、Mutation、Action 、 Getter 和 Modules等数据。

Tags

上一篇
没有啦
keep-alive
下一篇
keep-alive