fetch api vue js code example

Example 1: axios in vue

//Install Axios
npm install axios

//Import Axios in .vue file
import axios from 'axios'

//Add a method to implement Axios
testMethod () {
      axios.post('URL')
      .then(function (response) {
        alert (response.data);
      })
      .catch(function (error) {
        alert(error);
      });
    }

Example 2: vue fetch api

async created() {
  // GET request using fetch with async/await
  const response = await fetch("https://api.npms.io/v2/search?q=vue");
  const data = await response.json();
  this.totalVuePackages = data.total;
}

Example 3: how to use axios in vue

//Install Axios from terminal
npm install axios
//Import Axios in your HelloWorld.vue
import axios from 'axios'
//Add a method to implement Axios
test () {
      axios.post('URL')
      .then(function (response) {
        alert (response.data);
      })
      .catch(function (error) {
        alert(error);
      });
    }

Example 4: fetch in vue 3

# http requests using Fetcht api in Vue 3 explained (see videos below) 
https://www.youtube.com/watch?v=-Aoyja_BjZY
https://www.youtube.com/watch?v=LvOYCjpMQ10

Example 5: how to use api url in vue

<a :href="post.url" target="_blank"><img :src="post.image_url"></a>