snapshot of https://github.com/TanStack/query.git (main, 2026-09-16; blobs > 131072B omitted)
This commit is contained in:
6
examples/vue/dependent-queries/.gitignore
vendored
Normal file
6
examples/vue/dependent-queries/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
node_modules
|
||||
.DS_Store
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
package-lock.json
|
||||
6
examples/vue/dependent-queries/README.md
Normal file
6
examples/vue/dependent-queries/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# Dependent queries example
|
||||
|
||||
To run this example:
|
||||
|
||||
- `npm install` or `yarn`
|
||||
- `npm run dev` or `yarn dev`
|
||||
12
examples/vue/dependent-queries/index.html
Normal file
12
examples/vue/dependent-queries/index.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Vue Query Example</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/src/main.ts"></script>
|
||||
</body>
|
||||
</html>
|
||||
19
examples/vue/dependent-queries/package.json
Normal file
19
examples/vue/dependent-queries/package.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "@tanstack/query-example-vue-dependent-queries",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tanstack/vue-query": "^5.102.8",
|
||||
"vue": "^3.4.27"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@vitejs/plugin-vue": "^5.2.1",
|
||||
"typescript": "5.8.3",
|
||||
"vite": "^6.4.1"
|
||||
}
|
||||
}
|
||||
48
examples/vue/dependent-queries/src/App.vue
Normal file
48
examples/vue/dependent-queries/src/App.vue
Normal file
@@ -0,0 +1,48 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, ref } from 'vue'
|
||||
|
||||
import Posts from './Posts.vue'
|
||||
import Post from './Post.vue'
|
||||
|
||||
export default defineComponent({
|
||||
name: 'App',
|
||||
components: { Posts, Post },
|
||||
setup() {
|
||||
const visitedPosts = ref(new Set())
|
||||
const isVisited = (id: number) => visitedPosts.value.has(id)
|
||||
|
||||
const postId = ref(-1)
|
||||
const setPostId = (id: number) => {
|
||||
visitedPosts.value.add(id)
|
||||
postId.value = id
|
||||
}
|
||||
|
||||
return {
|
||||
isVisited,
|
||||
postId,
|
||||
setPostId,
|
||||
}
|
||||
},
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<h1>Vue Query - Dependent Queries</h1>
|
||||
<p>
|
||||
As you visit the posts below, you will notice them in a loading state the
|
||||
first time you load them. However, after you return to this list and click
|
||||
on any posts you have already visited again, you will see them load
|
||||
instantly and background refresh right before your eyes!
|
||||
<br />
|
||||
You will also notice, that after some time the name of the author gets
|
||||
loaded. This Data is not directly included in the
|
||||
<code>/post/id</code> response but gets fetched when the post gets loaded
|
||||
from its <code>userId</code>.
|
||||
<strong>
|
||||
(You may need to throttle your network speed to simulate longer loading
|
||||
sequences)
|
||||
</strong>
|
||||
</p>
|
||||
<Post v-if="postId > -1" :postId="postId" @setPostId="setPostId" />
|
||||
<Posts v-else :isVisited="isVisited" @setPostId="setPostId" />
|
||||
</template>
|
||||
72
examples/vue/dependent-queries/src/Post.vue
Normal file
72
examples/vue/dependent-queries/src/Post.vue
Normal file
@@ -0,0 +1,72 @@
|
||||
<script lang="ts">
|
||||
import { computed, defineComponent } from 'vue'
|
||||
import { useQuery } from '@tanstack/vue-query'
|
||||
|
||||
import { Author, Post } from './types'
|
||||
|
||||
const fetchPost = async (id: number): Promise<Post> =>
|
||||
await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`).then(
|
||||
(response) => response.json(),
|
||||
)
|
||||
|
||||
const fetchAuthor = async (id: number): Promise<Author> =>
|
||||
await fetch(`https://jsonplaceholder.typicode.com/users/${id}`).then(
|
||||
(response) => response.json(),
|
||||
)
|
||||
|
||||
export default defineComponent({
|
||||
name: 'PostDetails',
|
||||
props: {
|
||||
postId: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
emits: ['setPostId'],
|
||||
setup(props) {
|
||||
const {
|
||||
isPending,
|
||||
isError,
|
||||
isFetching,
|
||||
data: post,
|
||||
error,
|
||||
} = useQuery({
|
||||
queryKey: ['post', () => props.postId],
|
||||
queryFn: () => fetchPost(props.postId),
|
||||
})
|
||||
|
||||
const authorId = computed(() => post.value?.userId)
|
||||
|
||||
const { data: author } = useQuery({
|
||||
queryKey: ['author', authorId],
|
||||
queryFn: ({ queryKey: [, id] }) => fetchAuthor(id),
|
||||
enabled: computed(() => !!authorId.value),
|
||||
})
|
||||
|
||||
return { isPending, isError, isFetching, post, error, author }
|
||||
},
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<h1>Post {{ postId }}</h1>
|
||||
<a @click="$emit('setPostId', -1)" href="#"> Back </a>
|
||||
<div v-if="isPending" class="update">Loading...</div>
|
||||
<div v-else-if="isError">An error has occurred: {{ error }}</div>
|
||||
<div v-else-if="post">
|
||||
<h1>
|
||||
{{ post.title }} <span v-if="author">by {{ author.name }}</span>
|
||||
</h1>
|
||||
<div>
|
||||
<p>{{ post.body }}</p>
|
||||
</div>
|
||||
<div v-if="isFetching" class="update">Background Updating...</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.update {
|
||||
font-weight: bold;
|
||||
color: green;
|
||||
}
|
||||
</style>
|
||||
55
examples/vue/dependent-queries/src/Posts.vue
Normal file
55
examples/vue/dependent-queries/src/Posts.vue
Normal file
@@ -0,0 +1,55 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent } from 'vue'
|
||||
import { useQuery } from '@tanstack/vue-query'
|
||||
|
||||
import { Post } from './types'
|
||||
|
||||
const fetcher = async (): Promise<Post[]> =>
|
||||
await fetch('https://jsonplaceholder.typicode.com/posts').then((response) =>
|
||||
response.json(),
|
||||
)
|
||||
|
||||
export default defineComponent({
|
||||
name: 'PostsList',
|
||||
props: {
|
||||
isVisited: {
|
||||
type: Function,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
emits: ['setPostId'],
|
||||
setup() {
|
||||
const { isPending, isError, isFetching, data, error, refetch } = useQuery({
|
||||
queryKey: ['posts'],
|
||||
queryFn: fetcher,
|
||||
})
|
||||
|
||||
return { isPending, isError, isFetching, data, error, refetch }
|
||||
},
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<h1>Posts</h1>
|
||||
<div v-if="isPending">Loading...</div>
|
||||
<div v-else-if="isError">An error has occurred: {{ error }}</div>
|
||||
<div v-else-if="data">
|
||||
<ul>
|
||||
<li v-for="item in data" :key="item.id">
|
||||
<a
|
||||
@click="$emit('setPostId', item.id)"
|
||||
href="#"
|
||||
:class="{ visited: isVisited(item.id) }"
|
||||
>{{ item.title }}</a
|
||||
>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.visited {
|
||||
font-weight: bold;
|
||||
color: green;
|
||||
}
|
||||
</style>
|
||||
6
examples/vue/dependent-queries/src/main.ts
Normal file
6
examples/vue/dependent-queries/src/main.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { createApp } from 'vue'
|
||||
import { VueQueryPlugin } from '@tanstack/vue-query'
|
||||
|
||||
import App from './App.vue'
|
||||
|
||||
createApp(App).use(VueQueryPlugin).mount('#app')
|
||||
5
examples/vue/dependent-queries/src/shims-vue.d.ts
vendored
Normal file
5
examples/vue/dependent-queries/src/shims-vue.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
declare module '*.vue' {
|
||||
import { DefineComponent } from 'vue'
|
||||
const component: DefineComponent<{}, {}, any>
|
||||
export default component
|
||||
}
|
||||
30
examples/vue/dependent-queries/src/types.d.ts
vendored
Normal file
30
examples/vue/dependent-queries/src/types.d.ts
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
export interface Post {
|
||||
userId: number
|
||||
id: number
|
||||
title: string
|
||||
body: string
|
||||
}
|
||||
|
||||
export interface Author {
|
||||
id: number
|
||||
name: string
|
||||
username: string
|
||||
email: string
|
||||
address: {
|
||||
street: string
|
||||
suite: string
|
||||
city: string
|
||||
zipcode: string
|
||||
geo: {
|
||||
lat: string
|
||||
lng: string
|
||||
}
|
||||
}
|
||||
phone: string
|
||||
website: string
|
||||
company: {
|
||||
name: string
|
||||
catchPhrase: string
|
||||
bs: string
|
||||
}
|
||||
}
|
||||
15
examples/vue/dependent-queries/tsconfig.json
Normal file
15
examples/vue/dependent-queries/tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Bundler",
|
||||
"strict": true,
|
||||
"jsx": "preserve",
|
||||
"sourceMap": true,
|
||||
"resolveJsonModule": true,
|
||||
"esModuleInterop": true,
|
||||
"lib": ["esnext", "dom"],
|
||||
"types": ["vite/client"]
|
||||
},
|
||||
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
|
||||
}
|
||||
9
examples/vue/dependent-queries/vite.config.ts
Normal file
9
examples/vue/dependent-queries/vite.config.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [vue()],
|
||||
optimizeDeps: {
|
||||
exclude: ['@tanstack/vue-query', 'vue-demi'],
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user