|
| 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> |
0 commit comments