|
2 | 2 | div
|
3 | 3 | h1.title
|
4 | 4 | | life-cycle
|
5 |
| - p {{ message }} |
| 5 | + p asyncDataMessage: {{ asyncDataMessage }} |
| 6 | + p fetchMessage: {{this.$store.state.api.result}} |
| 7 | + p beforeCreateMessage: {{ beforeCreateMessage }} |
| 8 | + p createdMessage: {{ createdMessage }} |
| 9 | + p beforeMountMessage: {{ beforeMountMessage }} |
| 10 | + p mountMessage: {{ mountMessage }} |
| 11 | + p beforeDestroyMessage: {{ beforeDestroyMessage }} |
| 12 | + p destroyedMessage: {{ destroyedMessage }} |
6 | 13 | </template>
|
7 | 14 |
|
8 | 15 | <script lang="ts">
|
9 | 16 | import { Component, Vue } from 'nuxt-property-decorator'
|
| 17 | +import { AxiosRequestConfig } from 'axios' |
| 18 | +import { IApiPayload } from '@/interface/IApiPayload' |
10 | 19 |
|
11 | 20 | @Component
|
12 | 21 | export default class LifeCycle extends Vue {
|
13 |
| - message: string = 'open devTool!' |
| 22 | + // リクエスト用ペイロード |
| 23 | + public payload: IApiPayload = { |
| 24 | + hoge: 'foo' |
| 25 | + } |
| 26 | +
|
| 27 | + public asyncDataMessage: string = '' |
| 28 | + public beforeCreateMessage: string = '' |
| 29 | + public createdMessage: string = '' |
| 30 | + public beforeMountMessage: string = '' |
| 31 | + public mountMessage: string = '' |
| 32 | + public beforeDestroyMessage: string = '' |
| 33 | + public destroyedMessage: string = '' |
| 34 | +
|
| 35 | + // asyncData では return で data を返す必要がある |
| 36 | + // 逆にいうとこれ以外で vue に反映できない |
| 37 | + public async asyncData({ $axios }) { |
| 38 | + // this is undefined |
| 39 | + const { apiResult }: any = await $axios.$get( |
| 40 | + 'http://localhost:5000/api-waiting-for-5-seconds', |
| 41 | + { |
| 42 | + params: { |
| 43 | + hoge: 'fuga' |
| 44 | + } |
| 45 | + } as AxiosRequestConfig |
| 46 | + ) |
| 47 | +
|
| 48 | + return { |
| 49 | + asyncDataMessage: apiResult |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + // fetch は store を呼び出せるので、 vuex を使うことができる |
| 54 | + public async fetch({ store }) { |
| 55 | + // this is Vue instance |
| 56 | + await store.dispatch('api/fetchApi', this.payload) |
| 57 | + } |
14 | 58 |
|
15 | 59 | // https://ja.nuxtjs.org/guide/plugins
|
16 | 60 | // Vue インスタンスの ライフサイクル において、beforeCreate と created フックのみが クライアントサイドとサーバーサイドの両方 で呼び出されることに注意してください
|
17 | 61 | // それ以外のすべてのフックはクライアントサイドでのみ呼び出されます
|
18 |
| - beforeCreate() { |
| 62 | + public beforeCreate() { |
19 | 63 | console.log('beforeCreate')
|
| 64 | + this.beforeCreateMessage = 'beforeCreateMessage' |
20 | 65 | }
|
21 |
| - created() { |
| 66 | + // created では $el はまだ使えない |
| 67 | + public async created() { |
22 | 68 | console.log('created')
|
23 |
| - console.log('RUNTIME_ENV: ', process.env.RUNTIME_ENV) |
| 69 | + const { apiResult }: any = await this.$axios.$get( |
| 70 | + 'http://localhost:5000/api-waiting-for-5-seconds', |
| 71 | + { |
| 72 | + params: this.payload |
| 73 | + } as AxiosRequestConfig |
| 74 | + ) |
| 75 | + console.log('apiResult:', apiResult) |
| 76 | + this.createdMessage = apiResult |
| 77 | + console.log('created $el:', this.$el) // undefined |
24 | 78 | }
|
25 | 79 |
|
26 | 80 | // これ以降のメソッドはクライアントサイドでのみ実行されます
|
27 |
| - beforeMount() { |
| 81 | + public beforeMount() { |
28 | 82 | console.log('beforeMount')
|
| 83 | + this.beforeMountMessage = 'beforeMountMessage' |
29 | 84 | }
|
30 |
| - mounted() { |
| 85 | + // mounted では $el は使える |
| 86 | + public mounted() { |
31 | 87 | console.log('mounted')
|
| 88 | + this.mountMessage = 'mountMessage' |
| 89 | + console.log('mounted $el:', this.$el) |
32 | 90 | }
|
33 |
| - beforeDestroy() { |
| 91 | + public beforeDestroy() { |
34 | 92 | console.log('beforeDestroy')
|
| 93 | + this.beforeDestroyMessage = 'beforeDestroyMessage' |
35 | 94 | }
|
36 |
| - destroyed() { |
| 95 | + public destroyed() { |
37 | 96 | console.log('destroyed')
|
| 97 | + this.destroyedMessage = 'destroyedMessage' |
38 | 98 | }
|
39 | 99 |
|
40 | 100 | public head() {
|
|
0 commit comments