From the course: Learning Nuxt.js

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

The fetch hook

The fetch hook

- [Instructor] The fetch hook on the other hand can be used in any component. And it is called during server side rendering, right after the component instance is created. With the fetch hook, you have access to the component instance. Let's see another example using the fetch hook. Here in the index dot view file where we made the previous async data example, we will just stick that out and instead create a new async function called fetch. Now, here we don't actually need to pass in HTTP because we can access it from the component instance. So here I would call const response equals await. So now I would say this dot HTTP dot get and in here passing the URL for our endpoint. Now, before actually returning the response, one thing I would like to call out is that with the fetch hook, you actually need to merge the returned data to the data property or the component local state. Remember in asyncdata you did not have to do…

Contents