E
E
eugenedrvnk2020-08-20 17:20:01
API
eugenedrvnk, 2020-08-20 17:20:01

How to properly handle data from api in Vue?

The situation is that there are 3 different pages, a post page, a page for the last 3 posts, and a page for displaying all posts. On each of the pages, a request is made to receive posts and the question is how to process them correctly further.
I get a complete data set with all fields, some of which I don't need. I need to rotate it on each of the pages.
The question is how best to do it. In my head, it seems right to take "cleaning up dirty API data" into a separate function and import it into each of the pages and run the received data from the api through it. Or which option would be more correct? Simply duplicating the code of desire does not particularly arise.

That is, my version is such that everything would look something like this:

<script>
import {formatPosts} from 'handler.js'

export default {
  data() {
    return {
      posts: []
    }
  },
  
  created() {
    axios.get('/posts').then(response => {
      this.posts = formatPosts(response)
    })
  },
}
</script>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question