Skip to content

Commit 0109b8b

Browse files
committed
update counter hot example to use new API
1 parent ccafdbe commit 0109b8b

File tree

5 files changed

+33
-41
lines changed

5 files changed

+33
-41
lines changed

examples/counter-hot/Counter.vue

+9-8
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
</template>
1313

1414
<script>
15-
import store from './store'
15+
import * as actions from './store/actions'
16+
import { recentHistory } from './store/getters'
17+
1618
export default {
17-
computed: {
18-
count () {
19-
return store.state.count
19+
vuex: {
20+
state: {
21+
count: state => state.count,
22+
recentHistory
2023
},
21-
recentHistory: store.getters.recentHistory
22-
},
23-
methods: store.actions
24-
24+
actions: actions
25+
}
2526
}
2627
</script>

examples/counter-hot/main.js

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

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

examples/counter-hot/store/actions.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import { INCREMENT, DECREMENT } from './mutation-types'
22

3-
export default {
3+
export const increment = ({ dispatch }) => dispatch(INCREMENT)
4+
export const decrement = ({ dispatch }) => dispatch(DECREMENT)
45

5-
increment: INCREMENT,
6-
decrement: DECREMENT,
7-
8-
incrementIfOdd: ({ dispatch, state }) => {
9-
if ((state.count + 1) % 2 === 0) {
10-
dispatch(INCREMENT)
11-
}
12-
},
13-
14-
incrementAsync: ({ dispatch }) => {
15-
setTimeout(() => {
16-
dispatch(INCREMENT)
17-
}, 1000)
6+
export const incrementIfOdd = ({ dispatch, state }) => {
7+
if ((state.count + 1) % 2 === 0) {
8+
dispatch(INCREMENT)
189
}
1910
}
11+
12+
export const incrementAsync = ({ dispatch }) => {
13+
setTimeout(() => {
14+
dispatch(INCREMENT)
15+
}, 1000)
16+
}

examples/counter-hot/store/getters.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
export default {
2-
recentHistory (state) {
3-
const end = state.history.length
4-
const begin = end - 5 < 0 ? 0 : end - 5
5-
return state.history
6-
.slice(begin, end)
7-
.toString()
8-
.replace(/,/g, ', ')
9-
}
1+
export function recentHistory (state) {
2+
const end = state.history.length
3+
const begin = end - 5 < 0 ? 0 : end - 5
4+
return state.history
5+
.slice(begin, end)
6+
.toString()
7+
.replace(/,/g, ', ')
108
}

examples/counter-hot/store/index.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,14 @@ const state = {
1313

1414
const store = new Vuex.Store({
1515
state,
16-
actions,
17-
mutations,
18-
getters
16+
mutations
1917
})
2018

2119
if (module.hot) {
22-
module.hot.accept(['./actions', './mutations', './getters'], () => {
23-
const newActions = require('./actions').default
24-
const newMutations = require('./mutations').default
25-
const newGetters = require('./getters').default
20+
module.hot.accept(['./mutations'], () => {
21+
const mutations = require('./mutations').default
2622
store.hotUpdate({
27-
actions: newActions,
28-
mutations: newMutations,
29-
getters: newGetters
23+
mutations
3024
})
3125
})
3226
}

0 commit comments

Comments
 (0)