Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

Post.vue

46 lines1.1 KB

1<script lang="ts">
2import { useQuery } from '@tanstack/vue-query'
3import { defineComponent } from '@vue/composition-api'
4import { postsQueries } from './services/posts/queries'
5​
6export default defineComponent({
7 name: 'PostDetails',
8 props: {
9 postId: {
10 type: Number,
11 required: true,
12 },
13 },
14 emits: ['setPostId'],
15 setup(props) {
16 const { isPending, isError, isFetching, data, error } = useQuery(() =>
17 postsQueries.detail(props.postId),
18 )
19​
20 return { isPending, isError, isFetching, data, error }
21 },
22})
23</script>
24​
25<template>
26 <div>
27 <h1>Post {{ postId }}</h1>
28 <a @click="$emit('setPostId', -1)" href="#"> Back </a>
29 <div v-if="isPending" class="update">Loading...</div>
30 <div v-else-if="isError">An error has occurred: {{ error }}</div>
31 <div v-else-if="data">
32 <h1>{{ data.title }}</h1>
33 <div>
34 <p>{{ data.body }}</p>
35 </div>
36 <div v-if="isFetching" class="update">Background Updating...</div>
37 </div>
38 </div>
39</template>
40​
41<style scoped>
42.update {
43 font-weight: bold;
44 color: green;
46</style>