Skip to content

Instantly share code, notes, and snippets.

@larizzatg
Last active July 21, 2021 16:35
Show Gist options
  • Save larizzatg/dcff01be51d03900a613da4ff15fc4c0 to your computer and use it in GitHub Desktop.
Save larizzatg/dcff01be51d03900a613da4ff15fc4c0 to your computer and use it in GitHub Desktop.
Nuxt: Getting data, fetch method
<template>
<div>
<h1>Post</h1>
<div v-if="post" id="first-post">
<h3 class="text-lg">{{ post.title }}</h3>
<p class="text-base">{{ post.body }}</p>
</div>
</div>
</template>
<script>
import { defineComponent, ref, useContext, useFetch } from '@nuxtjs/composition-api';
export default defineComponent({
setup() {
const post = ref();
const { $axios } = useContext();
useFetch(async () => {
const { data } = await $axios.get('https://jsonplaceholder.typicode.com/posts/1');
post.value = data;
});
return { post };
},
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment