Skip to content

How to render error page from fetch hook? #9191

Discussion options

You must be logged in to vote

Hi,

Error handling in the fetch() hook occurs at component level, which means you can't directly render the error page as you would using asyncData().

fetch() provides a $fetchState object that you can use in your template to handle pending and error cases like so :

<div>
    <p v-if="$fetchState.pending">Fetching posts...</p>
    <p v-else-if="$fetchState.error">Error while fetching posts</p>
    <ul v-else></ul>
</div>

Take a look at this post for the details : https://nuxtjs.org/blog/understanding-how-fetch-works-in-nuxt-2-12/#error-handling

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by danielroe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2 participants