@@ -3,56 +3,22 @@ import Vuex from '../../src'
3
3
4
4
Vue . use ( Vuex )
5
5
6
- // mutation types
7
- // optional if you don't like constants.
8
- const INCREMENT = 'INCREMENT'
9
- const DECREMENT = 'DECREMENT'
10
-
11
6
// root state object.
12
7
// each Vuex instance is just a single state tree.
13
8
const state = {
14
9
count : 0
15
10
}
16
11
17
- // actions are what components will be able to
18
- // call as store.actions.xxx
19
- // note these are not the final functions the
20
- // components will be calling.
21
- const actions = {
22
-
23
- // for simple actions that just dispatches a single mutation,
24
- // we can just provide the mutation type.
25
- increment : INCREMENT ,
26
- decrement : DECREMENT ,
27
-
28
- // for a normal action function, it always recieves the store
29
- // instance as the first argument, from which we can get the
30
- // dispatch function and the state object. Any additional
31
- // arguments will follow the store argument.
32
- incrementIfOdd : ( { dispatch, state } ) => {
33
- if ( ( state . count + 1 ) % 2 === 0 ) {
34
- dispatch ( INCREMENT )
35
- }
36
- } ,
37
-
38
- // Same thing for async actions.
39
- incrementAsync : ( { dispatch } ) => {
40
- setTimeout ( ( ) => {
41
- dispatch ( INCREMENT )
42
- } , 1000 )
43
- }
44
- }
45
-
46
12
// mutations are operations that actually mutates the state.
47
13
// each mutation handler gets the entire state tree as the
48
14
// first argument, followed by additional payload arguments.
49
15
// mutations must be synchronous and can be recorded by middlewares
50
16
// for debugging purposes.
51
17
const mutations = {
52
- [ INCREMENT ] ( state ) {
18
+ INCREMENT ( state ) {
53
19
state . count ++
54
20
} ,
55
- [ DECREMENT ] ( state ) {
21
+ DECREMENT ( state ) {
56
22
state . count --
57
23
}
58
24
}
@@ -61,12 +27,11 @@ const mutations = {
61
27
// and the mutations. Because the actions and mutations are just
62
28
// functions that do not depend on the instance itself, they can
63
29
// be easily tested or even hot-reloaded (see counter-hot example).
64
- //
30
+ //
65
31
// You can also provide middlewares, which is just an array of
66
32
// objects containing some hooks to be called at initialization
67
33
// and after each mutation.
68
34
export default new Vuex . Store ( {
69
35
state,
70
- actions,
71
36
mutations
72
37
} )
0 commit comments