Last active
July 21, 2021 16:34
-
-
Save larizzatg/92a61422fccd25f84cdf4e2d934571fa to your computer and use it in GitHub Desktop.
Nuxt: Getting data, composition api with useAsync
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, useAsync, useContext } from '@nuxtjs/composition-api'; | |
export default defineComponent({ | |
setup() { | |
const { $axios } = useContext(); | |
const post = useAsync(async () => { | |
console.log('useAsync'); | |
const { data } = await $axios.get('https://jsonplaceholder.typicode.com/posts/1'); | |
return data; | |
}); | |
return { post }; | |
}, | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment