Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nuxt Content & vue, Fetching blog content not working in a component

I’m trying to fetch content from my content/blog folder but I can’t make it work in the components/menu.vue page since it’s a component ?

This is working in the page/index.vue but not in components/menu.vue. Why?

I’m new to all this and maybe I’m doing something wrong here, maybe I’m not able to fetch content from a component… I don’t know.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


<template>
  <div class="container">
    <div class="articles">
          <div class="article" v-for="article of articles" :key="article">
            <nuxt-link :to="{ name: 'slug', params: { slug: article.slug } }">
              <div class="article-inner">
                <img :src="require(`~/assets/${article.img}`)" alt="" />
                <div class="detail">
                  <h3>{{ article.title }}</h3>
                  <p>{{ article.description }}</p>
                </div>
              </div>
            </nuxt-link>
          </div>
     </div>
  </div>
</template>

<script>
export default {
  async asyncData ({ $content, params }) {
    const articles = await $content('blog', params.slug)
      .only(['title', 'description', 'img', 'slug'])
      .sortBy('createdAt', 'asc')
      .fetch()
    console.log('bongo')
    return {
      articles
    }
  },
  data () {
    return {
      num: this.$route.name
    }
  },
  computed: {
    currentRouteName () {
      return this.$route.name
    }
  }
}
</script>

>Solution :

As written here: https://nuxtjs.org/docs/features/data-fetching#async-data

asyncData is only available for pages and you don’t have access to this inside the hook.

So yes you can use asyncData only in a page.
An alternative would be to use a non-blocking fetch() hook as shown here: https://nuxtjs.org/docs/features/data-fetching#accessing-the-fetch-state

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading