LoginSignup
2
0

More than 3 years have passed since last update.

[備忘録] nuxt-property-decorator使用時にwatchQueryプロパティを使用する

Posted at

備忘録のためコードのみ
あとで直したい

コード

<template>
  <article class="hoge-page">
    <div>
      {{hogehoge}}
    </div>
    <nuxt-link :to="/hoge?page=2">2ページへ</nuxt-link>
  </article>
</template>
​
​
<script lang="ts">
import { Component, Vue, Prop } from 'nuxt-property-decorator'

@Component({
  components: {},
  watchQuery: ['page']
})
export default class Hoge extends Vue {

  hogehoge:string = '初期値'
  mogemoge:num[] = []
  
  //SSR時とnuxt-linkが押下時に実行される
  asyncData({ query }) {
    const pageNum = !query.page? 1 :query.page //クエリーがあればその数値を、なければ1を代入
    const data = hogehogeApi
      .fetch(pageNum)
      .then(res => {
        return {
          //上で定義したdataに上書きされる
          hogehoge: res.hogehoge,
          mogemoge: res.mogemoge
        }
      })
      .catch(e => {
        console.log(e)
      })
    return data 
  }
}
</script>
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0