Vuex有五个核心概念:
state, getters, mutations, actions, modules。
1. state:vuex的基本数据,用来存储变量
state: {
userId: '',
name: '',
token: '',
}
在vue中使用 this.$store.state.userId
2. geeter:从基本数据(state)派生的数据,相当于state的计算属性,具有返回值的方法
getter: {
userIdDouble: function(state){
return state.userId * 2
}
getter: {
userIdDouble: function(state){
return state.userId * 2
}
在vue中使用 this.$store.getters.userIdDouble
3. mutation:提交更新数据的方法,必须是同步的(如果需要异步使用action)。每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)。
mutations: {