

状态(State):Vuex 中的状态是存储应用程序数据的地方,通常表示为 JavaScript 对象。
获取器(Getters):获取器用于根据当前状态计算派生状态,类似于存储库中的计算属性。
突变(Mutations):突变是修改状态的唯一方式。它们是同步的,通过明确定义状态如何改变,有助于维护可预测的状态。
动作(Actions):动作用于执行异步操作和触发突变。适合进行诸如发出 API 请求然后根据结果提交突变等任务。
模块(Modules):Vuex 允许将存储划分为模块。这对于较大的应用程序有助于将状态、突变、动作和获取器组织成更小、更可管理的部分。
Vuex 特别适用于较大的应用程序,其中状态管理可能变得复杂。
它强制执行单向数据流,并使更容易跟踪应用程序状态变化的来源。这种可预测性有助于调试和维护不断增长的应用程序。

安装(Installation):你需要将 Vuex 安装为依赖项,可以使用 npm 或 yarn 进行安装。
npm i vuex@3 # vue2适用
存储配置(Store Configuration):通过定义状态、突变、动作和获取器来创建一个存储。

// store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
// 在这里定义应用程序的状态
},
mutations: {
// 在这里定义突变函数
},
actions: {
// 在这里定义动作函数
},
getters: {
// 在这里定义获取器函数
}
})
export default store
在 main.js 中导入挂载到 Vue 实例上
import Vue from 'vue'
import App from './App.vue'
import store from './store'
Vue.config.productionTip = false
new Vue({
render: h => h(App),
store
}).$mount('#app')
测试打印Vuex
created(){
console.log(this.$store)
}
const store = new Vuex.Store({
// state 状态, 即数据, 类似于vue组件中的data,
// 区别:
// 1.data 是组件自己的数据,
// 2.state 中的数据整个vue项目的组件都能访问到
state: {
user: null,
settings: {
theme: 'light',
language: 'en',
},
// 其他应用程序状态
},
// 其他配置项
})
Centralized: Vuex 中的状态是集中管理的,这意味着所有组件都可以访问相同的状态数据,而不需要通过复杂的组件传递数据来实现共享。
Reactive: Vuex 的状态是响应式的。当状态发生变化时,依赖于该状态的组件会自动更新以反映这些变化。
Read-Only: Vuex 的状态是只读的。这意味着你不能直接在组件中修改状态,而是需要通过 mutations 来进行修改。这有助于维护状态的可预测性。
Single Source of Truth: Vuex 鼓励将应用程序的状态集中到一个单一的状态树中,使其成为整个应用程序的“唯一数据源”。这有助于简化状态的管理和维护。
Predictable: 由于状态的修改只能通过 mutations 来进行,因此状态的变化变得可预测,易于调试和维护。
$store直接访问 —> {{ $store.state.count }}辅助函数mapState 映射计算属性 —> {{ count }}获取 store:
1.Vue模板中获取 this.$store
2.js文件中获取 import 导入 store
模板中: {{ $store.state.xxx }}
组件逻辑中: this.$store.state.xxx
JS模块中: store.state.xxx
this.$store.state 来获取状态的值<template>
<div>
<p>User: {{ $store.state.user }}</p>
<p>Theme: {{ $store.state.settings.theme }}</p>
</div>
</template>
<h1>state的数据 - {{ count }}</h1>
// 把state中数据,定义在组件内的计算属性中
computed: {
count () {
return this.$store.state.count
}
}
//main.js
import store from "@/store"
console.log(store.state.count)

import { mapState } from 'vuex'
mapState(['count'])
count () {
return this.$store.state.count
}
computed: {
...mapState(['count'])
}
<div> state的数据:{{ count }}</div>

button @click="handleAdd">值 + 1</button>
methods:{
handleAdd (n) {
// 错误代码(vue默认不会监测,监测需要成本)
this.$store.state.count++
// console.log(this.$store.state.count)
},
}
strict: true 可以开启严格模式,开启严格模式后,直接修改state中的值会报错
const store = new Vuex.Store({
state: {
count: 0
},
// 定义mutations
mutations: {
}
})
mutations: {
// 方法里参数 第一个参数是当前store的state属性
// payload 载荷 运输参数 调用mutaiions的时候 可以传递参数 传递载荷
addCount (state) {
state.count += 1
}
}
this.$store.commit('addCount')
this.$store.commit('xxx', 参数)mutations: {
...
addCount (state, count) {
state.count = count
}
}
handle ( ) {
this.$store.commit('addCount', 10)
}
this.$store.commit('addCount', {
count: 10
})
import { mapMutations } from 'vuex'
methods: {
...mapMutations(['addCount'])
}
//等价于
methods: {
// commit(方法名, 载荷参数)
addCount () {
this.$store.commit('addCount')
}
}
<button @click="addCount">值+1</button>
actions是用于处理异步操作的,例如从服务器获取数据或执行复杂的计算。Actions可以包含任何异步操作,但是它们最终需要调用mutations来更新state中的数据。
Actions是通过dispatch方法来调用的,dispatch方法接收一个action的名称和一个可选的payload参数。当调用dispatch方法时,它会触发一个action,并且可以在action中执行任何异步操作。
mutations: {
changeCount (state, newCount) {
state.count = newCount
}
}
actions: {
setAsyncCount (context, num) {
// 一秒后, 给一个数, 去修改 num
setTimeout(() => {
context.commit('changeCount', num)
}, 1000)
}
}
setAsyncCount () {
this.$store.dispatch('setAsyncCount', 666)
}

import { mapActions } from 'vuex'
methods: {
...mapActions(['changeCountAction'])
}
//mapActions映射的代码 本质上是以下代码的写法
//methods: {
// changeCountAction (n) {
// this.$store.dispatch('changeCountAction', n)
// },
//}
<button @click="changeCountAction(200)">+异步button>
| Mutations | Actions | |
|---|---|---|
| 目的 | 修改state中的数据 | 执行异步操作、调用多个mutations |
| 同步/异步 | 同步操作 | 可以是同步或异步操作 |
| 使用场景 | 更新state中的数据 | 执行异步请求、处理复杂逻辑 |
| 调用方式 | 使用commit方法调用 | 使用dispatch方法调用 |
| 响应 | 不能返回任何值,只能修改state | 可以返回Promise或异步操作的结果 |
| 跟踪 | 可以在devtools中跟踪mutations的调用 | 可以在devtools中跟踪actions的调用 |
const getters = {
//getters: {
// getters函数的第一个参数是 state
// 必须要有返回值
doneTodos: state => {
return state.todos.filter(todo => todo.done)
}
}
{{ $store.getters.doneTodos}}
import { mapGetters } from 'vuex'
export default {
computed: {
...mapGetters([
'doneTodos'
])
}
}
| State | Getters | Mutations | Actions | |
|---|---|---|---|---|
| 用途 | 存储应用程序的数据 | 从state中派生出计算属性 | 修改state中的数据 | 执行异步操作、调用多个mutations |
| 直接访问 | 使用this.$store.state | 使用this.$store.getters | 不直接访问,通过commit方法调用 | 不直接访问,通过dispatch方法调用 |
| 用法示例 | this.$store.state.count | this.$store.getters.doneTodos | this.$store.commit(‘increment’) | this.$store.dispatch(‘fetchData’) |
| 参数 | 无 | 接收state作为参数 | 接收state和payload作为参数 | 接收context对象作为参数 |
| 返回值 | 无 | 返回基于state的计算属性或派生数据 | 无返回值,只能修改state | 可以返回Promise或异步操作的结果 |
| 异步操作 | 不适用 | 不适用 | 不适用 | 适用 |
| 跟踪 | 无 | 可以在devtools中跟踪getters的调用 | 可以在devtools中跟踪mutations的调用 | 可以在devtools中跟踪actions的调用 |