2

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.

<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>
4
  • 1
    Hi, why did you deleted your previous question? I was about to answer how to fix the issue. Commented Aug 13, 2022 at 19:02
  • 1
    Sorry kissu! I solved the issue all by myself, it was a typo... I said import Opened from './Opened.vue' instead of ./Opened.vue Thank you very much for the support actually. It's very helpful ! :) Commented Aug 13, 2022 at 19:19
  • The issue was not related to the async call you had? Interesting. Commented Aug 13, 2022 at 19:21
  • the async call was working, I just need to write my code better... haha. Commented Aug 13, 2022 at 19:28

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks kissu for your help. Will dig into that since I have no clue on how to achieve that haha.
@damian it's just a bit different in the syntax, not a huge deal. You can create a new question if you'd like.
I nailed it by copying the result of this github.com/nuxt/content/issues/97 I would prefer to save some more important questions for later haha. Thanks a lot for your fast help.
I was wondering, since you are helping me a lot, if I could have a little help in private for that specific website? Thanks :)
@Aurore my bio is a place where you could find some info on how to reach me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.