vuex
可以帮助开发者管理共享状态,也就是管理全局变量
vuex
的几个核心概念:
vuex
使用一个store
对象管理应用的状态,一个store
包括:state
、getter
、mutation
、action
四个属性。
state
:state
意为“状态”,是vuex
状态管理的数据源。
getter
:getter
的作用与filters有一些相似,也可以将state进行过滤后输出。
mutation
:mutation
是vuex
中改变state的唯一途径,并且只能同步操作。
action
:一些对state
的异步操作可以放在action
中,并且通过在action
提交mutation
变更状态。
module
:当store
对象过于庞大时,可以根据具体的业务需求分为多个module。
我们可以在组件中触发 Action
,Action
则会提交 Mutation
,Mutation
会对 State
进行修改,组件再根据 State
、Getter
渲染页面
单文件的store
如下:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
},
mutations: {
},
actions: {
},
getter:{
},
modules: {
}
})