Skip to content

Instantly share code, notes, and snippets.

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