forked from ljianshu/Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
人心思动
authored
Aug 26, 2018
1 parent
705ba4d
commit a2f57ea
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 封装代码,让外部可以引用 |