Skip to content

Instantly share code, notes, and snippets.

@cursosdesarrolloweb
Created October 3, 2020 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cursosdesarrolloweb/9fbf0effabca4d32f986979d68b8db26 to your computer and use it in GitHub Desktop.
Save cursosdesarrolloweb/9fbf0effabca4d32f986979d68b8db26 to your computer and use it in GitHub Desktop.
Vue.component('fetch', {
data () {
return {
users: []
}
},
async mounted() {
const response = await fetch('https://jsonplaceholder.typicode.com/users');
this.users = await response.json()
}
},
template: `
<div>
<h2>Fetch con Vue 2</h2>
<ul v-if="users.length">
<li v-for="user in users" :key="user.id">
{{ user.name }} - {{ user.email }}
</li>
</ul>
<p v-else>No hay usuarios disponibles</p>
</div>
`
})
app.component("fetch", {
setup () {
const users = ref([]);
onMounted(async () => {
const response = await fetch("https://jsonplaceholder.typicode.com/users");
users.value = await response.json()
});
return { users };
},
template: `
<h2>Fetch en Vue 3</h2>
<ul v-if="users.length">
<li v-for="user in users" :key="user.id">
{{ user.name }} - {{ user.email }}
</li>
</ul>
<p v-else>No hay usuarios disponibles</p>
`
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment