1

I am in pages/index.vue and for show news in my page, i use asyncdata method but when i goto an other page and come back to home news data are empty. Should i save data on store? Or have other solution for this?

   <template>
      <div>
         {{news.length}}

         <nuxt-link to="/about"> about </nuxt-link>
      </div>
   </template>

   <script>
      async asyncData({$axios}){
        return $axios.post("/news")
           .then(({data : {data : news}}) => {
             return {news};
           })
           .catch(err => console.log(err));
        }
   </script>

2 Answers 2

2

Yea, usually we store it in a vuex store. It could look something like this:

  async asyncData({$axios, store, error}){
      try {
         let news = (await $axios.post("/news")).data.data.news;
         store.commit("SAVE_NEWS", news);
      catch (err) {
         error({ statusCode: 404, message: 'Cannot find })
      }
   },
   computed: {
      news() {
         return this.$store.state.news
      }
   }
Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't this have a negative effect on SEO?
@MortezaNegahi asyncData also have the error property. No it does not have a negative effect on SEO.
1

When you use .env in nuxt.config like this

env: {
    appUrl: "localhost:2323"
}

and use env parameters in axios config on nuxt.config like this

axios: {
    baseUrl : process.env.appUrl
}

asyncData doesn`t work on navigation change, And you must set baseUrl directly hardcode.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.