LoginSignup
6
5

More than 3 years have passed since last update.

Nuxt.jsでheadにasyncDataの値を入れたい

Posted at
hoge.vue
<script>
export default {
  async asyncData({ route, app, store, payload }) {
      return {
        title: payload.title.rendered
      }
    }
  },
  head: {
    title: this.title
  },

これではTypeError: Cannot read property 'title' of undefinedとなり失敗してしまう。

  head() {
    return { title: this.title }
  },

これで設定できた。headメソッドを使うことでコンポーネント内のデータにthisでアクセスできるようになる。nuxt.config.jsでメタ情報を設定するときと同じようにhead: { ...と書いてしまったのが失敗した原因のようだった。

参考:API: head メソッド - NuxtJS

6
5
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
6
5