包含对象

state、mutations、getters、actions

state

Vuex 中的数据源,可以通过 this.$store.state.变量名 访问仓库的数据源

mutations

通过 this.$store.commit(‘方法名’) 修改仓库中的数据源信息,同步操作

getters

理解为 vue 中的 computed

actions

类似 mutations,异步操作

使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const state={
number:1
}

const mutations={
addFunction(state){
return state.number+=1;
}
}

this.$store.state.number;
this.$store.commit('addFunction');

const getters={
//通过方法触发 state 是上面的 state对象,能读取state的值
addFunction(state){
return state.number++;
}
}
this.$store.getters.addFunction

const actions={
addFunction(context){
context.commit("addFunction");
}
}
this.$store.dispatch("addFunction")