How to access to the vue store in the asyncData function of nuxt

yoyojs picture yoyojs · Jul 27, 2018 · Viewed 15.2k times · Source

in a component i want to acces to the store with the asyncData function like so :

asyncData({ app, params }) {
var url = `https://myapi/news/${app.$store.state.market}/detail/${params.id}`;
return app.$axios.get(url).then(response => {
  return { actu: response.data };
});

}

but i received "Cannot read property 'state' of undefined"

is there another to receive the state of the store here ?

Answer

Aldarund picture Aldarund · Jul 27, 2018

You need to get store from context. Reference

asyncData({ app, params, store }) {
   var url = `https://myapi/news/${store.state.market}/detail/${params.id}`;
   return app.$axios.get(url).then(response => {
      return { actu: response.data };
});