Frontend State Management with Vue and Vuex : Testing your API in the Vue component

Testing your API in the Vue component

We can make a simple API in our App.vue component to verify if we are connected to the API source. Let's make a call to say the business API endpoint

http://localhost:3000/api/news/business

<template>
  <div id="app">
    <ul id="example-1">
      <li v-for="item in newsItems" :key="item.title">{{ item.title }}		    </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: "app",
  components: {},
  data() {
    return {
      newsItems: null
    };
  },
  created() {
    fetch("http://localhost:3000/api/news/business")
      .then(data => data.json())
      .then(data => (this.newsItems = data));
  }
};
</script>

<style></style>

 

This should display the array of news items retrieved in the template as shown below.

 

 


course logo
Frontend State Management with Vue and Vuex
Number of sections:
15
Content length:
Delivery formats:

Instructor

F Yeboah
I am a Software / UI developer with interests in Search Intensive applications, Information Architectures(IA), UX/UI, FRP, and Reusable Frontend Components Design.... Show more