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

Commit 9c2f834

Browse files
committed
😤 feat: add counter of store and vue
1 parent 1f75896 commit 9c2f834

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

src/pages/example/counter.vue

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<template lang="pug">
2+
div
3+
h1.title
4+
| counter
5+
p counter: {{ counter }} {{ this.$store.state.counter.count }}
6+
p isOdd: {{ isOdd }} {{ this.$store.getters['counter/isOdd'] }}
7+
div
8+
p Clicked: {{ value }} times
9+
p
10+
button(@click="onIncrement") +
11+
button(@click="onDecrement") -
12+
button(@click="onIncrementIfOdd") Increment if odd
13+
button(@click="onIncrementAsync") Increment async
14+
</template>
15+
16+
<script lang="ts">
17+
import { Component, Vue } from 'nuxt-property-decorator'
18+
import { RootStore } from '@/@types/vuex'
19+
20+
@Component
21+
export default class Counter extends Vue {
22+
$store!: RootStore
23+
24+
counter: number = 0
25+
26+
/** computed */
27+
public get isOdd() {
28+
return this.$store.getters['counter/isOdd']
29+
}
30+
31+
/** Nuxt ライフサイクル */
32+
public async asyncData({ store }: any) {
33+
await console.log('counter/asyncData')
34+
const { state } = store as RootStore
35+
const { count } = state.counter
36+
37+
return {
38+
counter: count + 1
39+
}
40+
}
41+
42+
/** Nuxt ライフサイクル */
43+
public async fetch({ store }: any): Promise<void> {
44+
await console.log('counter/fetch')
45+
const { state, commit, dispatch } = store as RootStore
46+
const { count } = state.counter
47+
48+
await dispatch('counter/asyncUpdateCount', count + 19)
49+
}
50+
51+
/** Vue ライフサイクル */
52+
public created() {
53+
setTimeout(() => {
54+
this.counter = this.increment()
55+
}, 3000)
56+
}
57+
58+
/** Vue ライフサイクル */
59+
public mounted() {
60+
console.log('mounted:', this.$refs)
61+
62+
setTimeout(async () => {
63+
await this.asyncCount(200)
64+
}, 5000)
65+
}
66+
67+
public increment() {
68+
const { state, commit } = this.$store
69+
commit('counter/updateCount', state.counter.count + 1)
70+
71+
// updated counter count
72+
console.log('counter/increment', state.counter.count)
73+
return state.counter.count
74+
}
75+
76+
public async asyncCount(count: number) {
77+
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)
83+
}
84+
85+
public onIncrement(e) {}
86+
87+
public onDecrement(e) {}
88+
89+
public onIncrementIfOdd(e) {}
90+
91+
public onIncrementAsync(e) {}
92+
93+
public head() {
94+
return {
95+
title: 'counter'
96+
}
97+
}
98+
}
99+
</script>
100+
101+
<style lang="scss" scoped></style>

src/pages/example/index.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ section
6565
nuxt-link(to='/example/video', no-prefetch) video
6666
p
6767
nuxt-link(to='/example/server-side-set-cookie', no-prefetch) server-side-set-cookie
68+
p
69+
nuxt-link(to='/example/counter', no-prefetch) counter
6870
</template>
6971

7072
<script lang="ts">

src/store/counter.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import Vue from 'vue'
2+
import {
3+
Convertor,
4+
DefineActionContext,
5+
DefineStoreModule
6+
} from '@lollipop-onl/vuex-typesafe-helper'
7+
8+
// Only define interface of state
9+
export interface IState {
10+
count: number
11+
}
12+
export const state = (): IState => ({
13+
count: 0
14+
})
15+
16+
// Convert to global name
17+
// It is an error if there is excess or deficiency
18+
export const getters = {
19+
isOdd: (state: IState): number => {
20+
return state.count % 2
21+
}
22+
}
23+
export type Getters = Convertor<
24+
typeof getters,
25+
{
26+
'counter/isOdd': 'isOdd'
27+
}
28+
>
29+
30+
export const mutations = {
31+
updateCount(state: IState, count: number): void {
32+
state.count = count
33+
},
34+
resetCount(state: IState): void {
35+
state.count = 0
36+
}
37+
}
38+
export type Mutations = Convertor<
39+
typeof mutations,
40+
{
41+
'counter/updateCount': 'updateCount'
42+
'counter/resetCount': 'resetCount'
43+
}
44+
>
45+
46+
export type Ctx = DefineActionContext<IState, typeof getters, typeof mutations>
47+
export const actions = {
48+
async asyncUpdateCount(
49+
this: Vue,
50+
{ commit }: Ctx,
51+
count: number
52+
): Promise<void> {
53+
await commit('updateCount', count)
54+
}
55+
}
56+
export type Actions = Convertor<
57+
typeof actions,
58+
{
59+
'counter/asyncUpdateCount': 'asyncUpdateCount'
60+
}
61+
>
62+
63+
// Define store module type
64+
export type Store = DefineStoreModule<
65+
'counter',
66+
IState,
67+
Getters,
68+
Mutations,
69+
Actions
70+
>

0 commit comments

Comments
 (0)