Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
人心思动 authored Aug 26, 2018
1 parent 705ba4d commit a2f57ea
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions vuex-demo/优化后store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {// 包含了多个直接更新state函数的对象
increment(state) {
state.count = state.count + 1;
},
decrement(state) {
state.count = state.count - 1;
}
},
getters: { // 当读取属性值时自动调用并返回属性值
evenOrOdd(state) {
return state.count % 2 === 0 ? "偶数" : "奇数";
}
},
actions: { // 包含了多个对应事件回调函数的对象
incrementIfOdd({ commit, state }) { // 带条件的action
if (state.count % 2 === 1) {
commit('increment')
}
},
incrementAsync({ commit }) { //异步的action
setInterval(() => {
commit('increment')
}, 2000);
}

}
})
export default store //用export default 封装代码,让外部可以引用

0 comments on commit a2f57ea

Please sign in to comment.