Fetch data from API

APIs are powerful tools that allow applications to communicate and exchange data. Vue.js, a progressive JavaScript framework, makes it easy to build dynamic user interfaces. When combined with Axios, a versatile HTTP client, Vue.js enables seamless data retrieval and rendering from APIs. By leveraging Vue.js and Axios, developers can create applications that fetch and display data in real-time, enhancing the user experience.

async function logMovies() {
  const response = await fetch("http://example.com/movies.json");
  const movies = await response.json();
  console.log(movies);
}
Install axios package using vue-cli.

npm i axios vue-axios
<template>
<div>
<h1>{{ title }}</h1>
<ul>
<li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
</div>
</template>
<script>

export default {
   data() {
    return {
      title: '',
      items: []
    };
   },
   
    mounted() {
     axios.get('https://api.example.com/data')
     .then(response => {
     this.title = response.data.title;
     this.items = response.data.items;
   })
   
     .catch(error => {
        // Handle any errors that occurred during the API request
   });
  }
};

</script>

 

Leave a Reply