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

PostList.vue

47 lines1.0 KB

1<script setup lang="ts">
2import { useQuery, useQueryClient } from '@tanstack/vue-query'
3import { postsQueries } from './services/posts/queries'
4​
5defineEmits<{ setPostId: [id: number] }>()
6​
7const queryClient = useQueryClient()
8​
9const { isPending, isError, isFetching, data, error } = useQuery(
10 postsQueries.list(),
12</script>
13​
14<template>
15 <h1>Posts</h1>
16 <div v-if="isPending">Loading...</div>
17 <div v-else-if="isError">An error has occurred: {{ error }}</div>
18 <div v-else-if="data">
19 <ul>
20 <li v-for="item in data" :key="item.id">
21 <a
22 href="#"
23 :class="{
24 visited: queryClient.getQueryData(
25 postsQueries.detail(item.id).queryKey,
26 ),
27 }"
28 @click="$emit('setPostId', item.id)"
29 >{{ item.title }}</a
30 >
31 </li>
32 </ul>
33 <div v-if="isFetching" class="update">Background Updating...</div>
34 </div>
35</template>
36​
37<style scoped>
38.update {
39 font-weight: bold;
40 color: green;
42​
43.visited {
44 font-weight: bold;
45 color: green;
47</style>