Skip to content

Commit c40fbf9

Browse files
committed
Updated counter-hot and shopping-cart with getters example.
1 parent d3ddc02 commit c40fbf9

File tree

7 files changed

+41
-17
lines changed

7 files changed

+41
-17
lines changed

examples/counter-hot/Counter.vue

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
<template>
22
<div>
3-
Clicked: {{ count }} times
3+
Value: {{ count }}
44
<button @click="increment">+</button>
55
<button @click="decrement">-</button>
66
<button @click="incrementIfOdd">Increment if odd</button>
77
<button @click="incrementAsync">Increment async</button>
8+
<div>
9+
<div>Recent History: {{recentHistory}}</div>
10+
</div>
811
</div>
912
</template>
1013

1114
<script>
1215
import store from './store'
13-
1416
export default {
1517
computed: {
1618
count () {
1719
return store.state.count
18-
}
20+
},
21+
recentHistory: store.getters.recentHistory
1922
},
2023
methods: store.actions
24+
2125
}
2226
</script>

examples/counter-hot/store/getters.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}
10+
}

examples/counter-hot/store/index.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@ import Vue from 'vue'
22
import Vuex from '../../../src'
33
import actions from './actions'
44
import mutations from './mutations'
5+
import getters from './getters'
56

67
Vue.use(Vuex)
78

89
const state = {
9-
count: 0
10+
count: 0,
11+
history: []
1012
}
1113

1214
const store = new Vuex.Store({
1315
state,
1416
actions,
15-
mutations
17+
mutations,
18+
getters
1619
})
1720

1821
if (module.hot) {
19-
module.hot.accept(['./actions', './mutations'], () => {
22+
module.hot.accept(['./actions', './mutations', './getters'], () => {
2023
const newActions = require('./actions').default
2124
const newMutations = require('./mutations').default
25+
const newGetters = require('./getters').default
2226
store.hotUpdate({
2327
actions: newActions,
24-
mutations: newMutations
28+
mutations: newMutations,
29+
getters: newGetters
2530
})
2631
})
2732
}

examples/counter-hot/store/mutations.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { INCREMENT, DECREMENT } from './mutation-types'
33
export default {
44
[INCREMENT] (state) {
55
state.count++
6+
state.history.push('increment')
67
},
78
[DECREMENT] (state) {
89
state.count--
10+
state.history.push('decrement')
911
}
1012
}

examples/shopping-cart/components/Cart.vue

+1-10
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,7 @@ const { checkout } = store.actions
1919
2020
export default {
2121
computed: {
22-
products () {
23-
return store.state.cart.added.map(({ id, quantity }) => {
24-
const product = store.state.products.find(p => p.id === id)
25-
return {
26-
title: product.title,
27-
price: product.price,
28-
quantity
29-
}
30-
})
31-
},
22+
products: store.getters.cartProducts,
3223
checkoutStatus () {
3324
return store.state.cart.lastCheckout
3425
},
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function cartProducts (state) {
2+
return state.cart.added.map(({ id, quantity }) => {
3+
const product = state.products.find(p => p.id === id)
4+
return {
5+
title: product.title,
6+
price: product.price,
7+
quantity
8+
}
9+
})
10+
}

examples/shopping-cart/store/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Vue from 'vue'
22
import Vuex from '../../../src'
33
import * as actions from './actions'
4+
import * as getters from './getters'
45
import { cartInitialState, cartMutations } from './modules/cart'
56
import { productsInitialState, productsMutations } from './modules/products'
67

@@ -15,6 +16,7 @@ export default new Vuex.Store({
1516
products: productsInitialState
1617
},
1718
actions,
19+
getters,
1820
mutations: [cartMutations, productsMutations],
1921
strict: debug,
2022
middlewares: debug ? [Vuex.createLogger()] : []

0 commit comments

Comments
 (0)