Last active
July 21, 2021 16:35
-
-
Save larizzatg/dcff01be51d03900a613da4ff15fc4c0 to your computer and use it in GitHub Desktop.
Nuxt: Getting data, fetch method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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