Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vuejs2

nuxt.js

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 ?

like image 570
yoyojs Avatar asked Jul 27 '18 10:07

yoyojs


2 Answers

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 };
});
like image 107
Aldarund Avatar answered Nov 17 '22 08:11

Aldarund


This worked for me

Store/index.js

...

state: {
  loadedPages: []
}
...

Page

async asyncData(context) {
...
console.log(context.store.state.loadedPages)
...

}
like image 20
atazmin Avatar answered Nov 17 '22 09:11

atazmin