0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Grudsomeでmetadataを設定すると TypeError: Cannot read property '$page' of undefined. が発生する

Posted at

概要

metadataがページに依存するように最適化を行おうとした
templates内に設定したmetadataが読み込まれなかった

状況

BlogPost.vue
<script>
export default {
  metaInfo: {
    title: this.$page.blogPost.title,
    metaInfo: {
      meta: [
        {
          property: "og:site_name",
          content: "Gridsome OGP テスト",
        },
        {
          property: "og:type",
          content: "website",
        },
        {
          property: "og:url",
          content: "https://sample.com/Blog/",
        },
        {
          property: "og:title",
          content: this.$page.blogPost.title,
        },
        {
          property: "og:description",
          content: this.$page.blogPost.content,
        },
        {
          property: "og:image",
          content: "https://sample.com/sample.jpg",
        },
        {
          name: "twitter:card",
          content: "summary_large_image",
        },
      ],
    },
  },
};
</script>

ビルドは正常に行われるがブラウザから確認すると表示されず、また開発者ツールから確認すると下記のエラーが吐かれている

TypeError: Cannot read property '$page' of undefined

対応

BlogPost.vue
<script>
export default {
  metaInfo() {
    return {
      title: this.$page.blogPost.title,
      metaInfo: {
        meta: [
          {
            property: "og:site_name",
            content: "Gridsome OGP テスト",
          },
          {
            property: "og:type",
            content: "website",
          },
          {
            property: "og:url",
            content: "https://sample.com/Blog/",
          },
          {
            property: "og:title",
            content: this.$page.blogPost.title,
          },
          {
            property: "og:description",
            content: this.$page.blogPost.content,
          },
          {
            property: "og:image",
            content: "https://sample.com/sample.jpg",
          },
          {
            name: "twitter:card",
            content: "summary_large_image",
          },
        ],
      },
    };
  },
};
</script>

moreInfoを関数に各値をreturnで括ることでエラーがなくなりました

まだ触り始めたばかりなので正直なぜこれで大丈夫なのかがわかりません……

参考

Gridsomeでイチからブログを作る - metaタグやOGPを最適化する

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?