Skip to content

Commit ccafdbe

Browse files
committed
convert counter example to new API
1 parent 7844f51 commit ccafdbe

File tree

4 files changed

+26
-45
lines changed

4 files changed

+26
-45
lines changed

examples/counter/Counter.vue

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
</template>
1010

1111
<script>
12-
import store from './store'
12+
import * as actions from './actions'
1313
1414
export default {
15-
computed: {
16-
count () {
17-
return store.state.count
18-
}
19-
},
20-
methods: store.actions
15+
vuex: {
16+
state: {
17+
count: state => state.count
18+
},
19+
actions: actions
20+
}
2121
}
2222
</script>

examples/counter/actions.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const increment = ({ dispatch }) => dispatch('INCREMENT')
2+
export const decrement = ({ dispatch }) => dispatch('DECREMENT')
3+
4+
export const incrementIfOdd = ({ dispatch, state }) => {
5+
if ((state.count + 1) % 2 === 0) {
6+
dispatch('INCREMENT')
7+
}
8+
}
9+
10+
export const incrementAsync = ({ dispatch }) => {
11+
setTimeout(() => {
12+
dispatch('INCREMENT')
13+
}, 1000)
14+
}

examples/counter/main.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import Vue from 'vue'
22
import Counter from './Counter.vue'
3+
import store from './store'
34

45
new Vue({
56
el: 'body',
7+
store,
68
components: { Counter }
79
})

examples/counter/store.js

+3-38
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,22 @@ import Vuex from '../../src'
33

44
Vue.use(Vuex)
55

6-
// mutation types
7-
// optional if you don't like constants.
8-
const INCREMENT = 'INCREMENT'
9-
const DECREMENT = 'DECREMENT'
10-
116
// root state object.
127
// each Vuex instance is just a single state tree.
138
const state = {
149
count: 0
1510
}
1611

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-
4612
// mutations are operations that actually mutates the state.
4713
// each mutation handler gets the entire state tree as the
4814
// first argument, followed by additional payload arguments.
4915
// mutations must be synchronous and can be recorded by middlewares
5016
// for debugging purposes.
5117
const mutations = {
52-
[INCREMENT] (state) {
18+
INCREMENT (state) {
5319
state.count++
5420
},
55-
[DECREMENT] (state) {
21+
DECREMENT (state) {
5622
state.count--
5723
}
5824
}
@@ -61,12 +27,11 @@ const mutations = {
6127
// and the mutations. Because the actions and mutations are just
6228
// functions that do not depend on the instance itself, they can
6329
// be easily tested or even hot-reloaded (see counter-hot example).
64-
//
30+
//
6531
// You can also provide middlewares, which is just an array of
6632
// objects containing some hooks to be called at initialization
6733
// and after each mutation.
6834
export default new Vuex.Store({
6935
state,
70-
actions,
7136
mutations
7237
})

0 commit comments

Comments
 (0)