vuex.js 634 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import Vue from 'vue'
  2. import Vuex from '../../src'
  3. Vue.use(Vuex)
  4. const INCREMENT = 'INCREMENT'
  5. const DECREMENT = 'DECREMENT'
  6. const state = {
  7. count: 0
  8. }
  9. const actions = {
  10. increment: INCREMENT,
  11. decrement: DECREMENT,
  12. incrementIfOdd: () => (dispatch, state) => {
  13. if (state.count % 2 === 1) {
  14. dispatch(INCREMENT)
  15. }
  16. },
  17. incrementAsync: () => (dispatch, state) => {
  18. setTimeout(() => {
  19. dispatch(INCREMENT)
  20. }, 1000)
  21. }
  22. }
  23. const mutations = {
  24. [INCREMENT] (state) {
  25. state.count++
  26. },
  27. [DECREMENT] (state) {
  28. state.count--
  29. }
  30. }
  31. export default new Vuex({
  32. state,
  33. actions,
  34. mutations
  35. })