Vuex 是 Vue.js 的状态管理库,它提供了一种集中管理状态的方式,使得组件之间的状态共享变得简单和高效。Vuex 主要有以下几种核心属性:
一、Vuex 的核心属性
1:State:
Vuex 使用 state 来存储应用的状态。state 是一个普通的 JavaScript 对象,可以在 Vue 组件中访问。
const store = new Vuex.Store({
state: {
count: 0
}
});
2:Getters:
getters 是一种计算属性,允许你从 state 中派生出一些状态。它们是基于 state 计算得出的,支持缓存。
const store = new Vuex.Store({
state: {
count: 0
},
getters: {
doubleCount(state) {
return state.count * 2;
}
}
});
3:Mutations:
mutations 是用于改变 state 的唯一方法。每个 mutation