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

Commit 9526014

Browse files
authored
Merge pull request #21 from hisasann/feature/add-vuex-typesafe-helper
Feature/add vuex typesafe helper
2 parents 10dd24c + fa6bc33 commit 9526014

File tree

6 files changed

+186
-0
lines changed

6 files changed

+186
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"url": "https://github.com/hisasann/typescript-nuxtjs-boilerplate/issues"
4848
},
4949
"dependencies": {
50+
"@lollipop-onl/vuex-typesafe-helper": "^0.2.6",
5051
"@nuxt/typescript": "^2.8.0",
5152
"@nuxtjs/axios": "^5.5.3",
5253
"@nuxtjs/pwa": "^3.0.0-beta.14",

src/@types/vuex.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Store as CounterStore } from '@/store/counter'
2+
3+
export type RootStore = CounterStore

src/pages/example/counter.vue

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<template lang="pug">
2+
div
3+
h1.title
4+
| counter
5+
div
6+
p.clicked
7+
| Clicked:
8+
span.value {{ value }}
9+
span times
10+
p
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
15+
</template>
16+
17+
<script lang="ts">
18+
import { Component, Vue } from 'nuxt-property-decorator'
19+
import { RootStore } from '@/@types/vuex'
20+
21+
@Component
22+
export default class Counter extends Vue {
23+
$store!: RootStore
24+
25+
/** computed */
26+
public get value() {
27+
return this.$store.state.counter.count
28+
}
29+
30+
/** Nuxt ライフサイクル */
31+
public asyncData({ store }: any) {
32+
const { state } = store as RootStore
33+
const { count } = state.counter
34+
35+
// ...
36+
}
37+
38+
/** Nuxt ライフサイクル */
39+
public fetch({ store }: any) {
40+
const { state, commit, dispatch } = store as RootStore
41+
const { count } = state.counter
42+
43+
// ...
44+
}
45+
46+
public onIncrement() {
47+
console.log('onIncrement')
48+
const { state, commit } = this.$store
49+
commit('counter/increment', 1)
50+
}
51+
52+
public onDecrement() {
53+
console.log('onDecrement')
54+
const { state, commit } = this.$store
55+
commit('counter/decrement', 1)
56+
}
57+
58+
public onIncrementIfOdd() {
59+
console.log('onIncrementIfOdd')
60+
const { state, getters, commit } = this.$store
61+
62+
if (getters['counter/isOdd']) {
63+
this.onIncrement()
64+
}
65+
}
66+
67+
public async onIncrementAsync() {
68+
console.log('onIncrementAsync')
69+
const { state, dispatch } = this.$store
70+
await dispatch('counter/asyncUpdateCount', 1)
71+
}
72+
73+
public head() {
74+
return {
75+
title: 'counter'
76+
}
77+
}
78+
}
79+
</script>
80+
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/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+
increment(state: IState, count: number): void {
32+
state.count += count
33+
},
34+
decrement(state: IState, count: number): void {
35+
state.count -= count
36+
}
37+
}
38+
export type Mutations = Convertor<
39+
typeof mutations,
40+
{
41+
'counter/increment': 'increment'
42+
'counter/decrement': 'decrement'
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('increment', 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+
>

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,14 @@
12131213
dependencies:
12141214
core-js "^2.5.7"
12151215

1216+
"@lollipop-onl/vuex-typesafe-helper@^0.2.6":
1217+
version "0.2.6"
1218+
resolved "https://registry.yarnpkg.com/@lollipop-onl/vuex-typesafe-helper/-/vuex-typesafe-helper-0.2.6.tgz#a1e21e7acf50869bbc98f77406160191657368b4"
1219+
integrity sha512-4VpiliSKlyPiVXlCMVtQGg5zxFpkBeB7Oj6kmx1b/Vemlr/I4pQAVuW+S1/j0sRTUW0VYnZ2KvPfqI5lK0yZAw==
1220+
dependencies:
1221+
typescript "^3.4.5"
1222+
vuex "^3.1.1"
1223+
12161224
"@mrmlnc/readdir-enhanced@^2.2.1":
12171225
version "2.2.1"
12181226
resolved "https://registry.yarnpkg.com/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz#524af240d1a360527b730475ecfa1344aa540dde"
@@ -13043,6 +13051,11 @@ typedarray@^0.0.6:
1304313051
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
1304413052
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
1304513053

13054+
typescript@^3.4.5:
13055+
version "3.5.2"
13056+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.2.tgz#a09e1dc69bc9551cadf17dba10ee42cf55e5d56c"
13057+
integrity sha512-7KxJovlYhTX5RaRbUdkAXN1KUZ8PwWlTzQdHV6xNqvuFOs7+WBo10TQUqT19Q/Jz2hk5v9TQDIhyLhhJY4p5AA==
13058+
1304613059
typescript@^3.5.1:
1304713060
version "3.5.1"
1304813061
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.1.tgz#ba72a6a600b2158139c5dd8850f700e231464202"

0 commit comments

Comments
 (0)