2

I want to access the props inside the async fetch() but I'm also using async fetch(context). So, I'm not sure how to access the props.

2
  • 1
    What have you tried so far? Please be descriptive and clear about your problem, and please read how-to-ask Commented Apr 28, 2022 at 10:36
  • @AnindyaDey I'm really new to this thing. I don't know how to proceed with the code. Commented Apr 28, 2022 at 10:57

2 Answers 2

2

In Nuxt 2, you have 2 fetch hooks.

The old one, before Nuxt 2.12, fetch(context) which acts a lot like asyncData. It's executed before the component creation, so you don't have access to it (data, props, options... nothing).
This one is deprecated, use asyncData instead.

The new one, from Nuxt 2.12, fetch() (with no parameters). It's executed at the same time as the created() hook. It has access to the component's context (props, data, etc.).

fetch(context) {
  // "this" doesn't exists
  // context is the Vue global context
}

fetch() {
  this.myProp // "this" exists and have access to props
}
Sign up to request clarification or add additional context in comments.

1 Comment

Here are more details regarding the lifecycle of Nuxt: nuxtjs.org/docs/concepts/nuxt-lifecycle#nuxt-lifecycle
2

As shown in the documentation, you can access the context with this when using the fetch() hook like

async fetch() {
  await this.$axios // or whatever this.$i18n etc.....
}

Don't use fetch(context) as explained here.

Comments

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.