Skip to content
This repository was archived by the owner on Jul 10, 2019. It is now read-only.

Commit fa6bc33

Browse files
committed
🦑 feat: change increment code to redux/counter
https://github.com/reduxjs/redux/tree/master/examples/counter
1 parent 9c2f834 commit fa6bc33

File tree

2 files changed

+55
-59
lines changed

2 files changed

+55
-59
lines changed

src/pages/example/counter.vue

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
div
33
h1.title
44
| counter
5-
p counter: {{ counter }} {{ this.$store.state.counter.count }}
6-
p isOdd: {{ isOdd }} {{ this.$store.getters['counter/isOdd'] }}
75
div
8-
p Clicked: {{ value }} times
6+
p.clicked
7+
| Clicked:
8+
span.value {{ value }}
9+
span times
910
p
10-
button(@click="onIncrement") +
11-
button(@click="onDecrement") -
12-
button(@click="onIncrementIfOdd") Increment if odd
13-
button(@click="onIncrementAsync") Increment async
11+
button.button-size(@click="onIncrement") +
12+
button.button-size(@click="onDecrement") -
13+
button.button-size(@click="onIncrementIfOdd") Increment if odd
14+
button.button-size(@click="onIncrementAsync") Increment async
1415
</template>
1516

1617
<script lang="ts">
@@ -21,75 +22,54 @@ import { RootStore } from '@/@types/vuex'
2122
export default class Counter extends Vue {
2223
$store!: RootStore
2324
24-
counter: number = 0
25-
2625
/** computed */
27-
public get isOdd() {
28-
return this.$store.getters['counter/isOdd']
26+
public get value() {
27+
return this.$store.state.counter.count
2928
}
3029
3130
/** Nuxt ライフサイクル */
32-
public async asyncData({ store }: any) {
33-
await console.log('counter/asyncData')
31+
public asyncData({ store }: any) {
3432
const { state } = store as RootStore
3533
const { count } = state.counter
3634
37-
return {
38-
counter: count + 1
39-
}
35+
// ...
4036
}
4137
4238
/** Nuxt ライフサイクル */
43-
public async fetch({ store }: any): Promise<void> {
44-
await console.log('counter/fetch')
39+
public fetch({ store }: any) {
4540
const { state, commit, dispatch } = store as RootStore
4641
const { count } = state.counter
4742
48-
await dispatch('counter/asyncUpdateCount', count + 19)
43+
// ...
4944
}
5045
51-
/** Vue ライフサイクル */
52-
public created() {
53-
setTimeout(() => {
54-
this.counter = this.increment()
55-
}, 3000)
46+
public onIncrement() {
47+
console.log('onIncrement')
48+
const { state, commit } = this.$store
49+
commit('counter/increment', 1)
5650
}
5751
58-
/** Vue ライフサイクル */
59-
public mounted() {
60-
console.log('mounted:', this.$refs)
61-
62-
setTimeout(async () => {
63-
await this.asyncCount(200)
64-
}, 5000)
52+
public onDecrement() {
53+
console.log('onDecrement')
54+
const { state, commit } = this.$store
55+
commit('counter/decrement', 1)
6556
}
6657
67-
public increment() {
68-
const { state, commit } = this.$store
69-
commit('counter/updateCount', state.counter.count + 1)
58+
public onIncrementIfOdd() {
59+
console.log('onIncrementIfOdd')
60+
const { state, getters, commit } = this.$store
7061
71-
// updated counter count
72-
console.log('counter/increment', state.counter.count)
73-
return state.counter.count
62+
if (getters['counter/isOdd']) {
63+
this.onIncrement()
64+
}
7465
}
7566
76-
public async asyncCount(count: number) {
67+
public async onIncrementAsync() {
68+
console.log('onIncrementAsync')
7769
const { state, dispatch } = this.$store
78-
79-
await dispatch('counter/asyncUpdateCount', state.counter.count + count)
80-
81-
// updated counter count
82-
console.log('counter/asyncCount', state.counter.count)
70+
await dispatch('counter/asyncUpdateCount', 1)
8371
}
8472
85-
public onIncrement(e) {}
86-
87-
public onDecrement(e) {}
88-
89-
public onIncrementIfOdd(e) {}
90-
91-
public onIncrementAsync(e) {}
92-
9373
public head() {
9474
return {
9575
title: 'counter'
@@ -98,4 +78,20 @@ export default class Counter extends Vue {
9878
}
9979
</script>
10080

101-
<style lang="scss" scoped></style>
81+
<style lang="scss" scoped>
82+
.clicked {
83+
font-size: 1.5em;
84+
}
85+
86+
.value {
87+
color: red;
88+
margin: 0 5px 0 5px;
89+
}
90+
91+
.button-size {
92+
font-size: 1.2em;
93+
width: 170px;
94+
height: 100px;
95+
margin: 2px 2px;
96+
}
97+
</style>

src/store/counter.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ export type Getters = Convertor<
2828
>
2929

3030
export const mutations = {
31-
updateCount(state: IState, count: number): void {
32-
state.count = count
31+
increment(state: IState, count: number): void {
32+
state.count += count
3333
},
34-
resetCount(state: IState): void {
35-
state.count = 0
34+
decrement(state: IState, count: number): void {
35+
state.count -= count
3636
}
3737
}
3838
export type Mutations = Convertor<
3939
typeof mutations,
4040
{
41-
'counter/updateCount': 'updateCount'
42-
'counter/resetCount': 'resetCount'
41+
'counter/increment': 'increment'
42+
'counter/decrement': 'decrement'
4343
}
4444
>
4545

@@ -50,7 +50,7 @@ export const actions = {
5050
{ commit }: Ctx,
5151
count: number
5252
): Promise<void> {
53-
await commit('updateCount', count)
53+
await commit('increment', count)
5454
}
5555
}
5656
export type Actions = Convertor<

0 commit comments

Comments
 (0)