Nuxt.js: Is it possible to recall actions from asyncData?

Created on 25 Jan 2018  路  6Comments  路  Source: nuxt/nuxt.js

So, I have some axios call in asyncData, after while i would like to recall this action again, is it possible to call asyncData as a one big function again from created hook e.g.? Something like this.asyncData() ?

This question is available on Nuxt.js community (#c2334)

Most helpful comment

This is a tricky question @lukasborawski

It is possible but kind of tricky since you need to pass the context to the asyncData method, here how you can do it:

 async mounted() {
    const data = await this.$options.asyncData(this.$root.$options.context)
    console.log('data', data)
 }

this.$root.$options.context is actually the last context given to asyncData, but keep in mind that redirect won't work for example.

PS: You don't want to call asyncData from created hook since it's run on both client-side and server-side, you want to use it on client-side only (beforeMount and mounted).

All 6 comments

This is a tricky question @lukasborawski

It is possible but kind of tricky since you need to pass the context to the asyncData method, here how you can do it:

 async mounted() {
    const data = await this.$options.asyncData(this.$root.$options.context)
    console.log('data', data)
 }

this.$root.$options.context is actually the last context given to asyncData, but keep in mind that redirect won't work for example.

PS: You don't want to call asyncData from created hook since it's run on both client-side and server-side, you want to use it on client-side only (beforeMount and mounted).

REALLY useful workaround @atinux 馃憤

Hi guys,

This works after nuxt generate command??

This question has been resolved by @Atinux, see answer.

Well that should be @roqsantacruz

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bimohxh picture bimohxh  路  3Comments

danieloprado picture danieloprado  路  3Comments

uptownhr picture uptownhr  路  3Comments

mikekidder picture mikekidder  路  3Comments

shyamchandranmec picture shyamchandranmec  路  3Comments