vue fetching data from api code example
Example 1: vue fetch api
async created() {
const response = await fetch("https://api.npms.io/v2/search?q=vue");
const data = await response.json();
this.totalVuePackages = data.total;
}
Example 2: data fetching vue
<template>
<div>
<h1>{{ title }}</h1>
<ul>
<li v-for="user in users" :key="user.id">{{ user.id }}. {{ user.name }} - {{ user.email }}</li>
</ul>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: 'Users',
props: {
title: String
},
data: () => ({
users: []
}),
mounted() {
axios.get('https://jsonplaceholder.typicode.com/users').then((res) => {
this.users = res.data
})
}
}
</script>
<style scoped>
h1 {
color: black;
font-size: 24px;
font-weight: 200;
padding: 0px 10px 0px;
}
ul li {
list-style: none;
color: black;
font-weight: 400;
opacity: 0.8;
font-size: 18px;
}
</style>