Search code examples
vue.jsvue-componentnuxt.jsnuxt-content

How to use @nuxt/content inside Component?


I'm trying to nuxt-contnt inside a component it works well in page with asyncData but not working inside the component

this works fine :

export default {
 async asyncData({ $content }) {
 const page = await $content('hello').fetch()

 return {
   page,
  }
 },
}

but this is not working :

 export default {
 data() {
  return {
  content: [],
 }
 },
 async fetch({ $content }) {
  this.content = await $content('hello').fetch()

 },
}

Solution

  • fetch doesn't have any parameters but has access to this, so it should be

    async fetch() {
      this.content = await this.$content('hello').fetch()
     }
    

    https://nuxtjs.org/docs/features/data-fetching/