6
6
type TypesConfig ,
7
7
type RouteRecordName ,
8
8
} from ' unplugin-vue-router/types'
9
+ import { computed } from ' vue'
9
10
const a: RouteRecordName = ' /articles'
10
11
11
12
// import type { RouteLocationNormalized, _RouteLocationNormalized } from 'vue-router/auto'
@@ -80,7 +81,7 @@ const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
80
81
</script >
81
82
82
83
<script lang="ts" setup>
83
- import { useQuery } from ' @tanstack/vue-query'
84
+ import { useMutation , useQuery } from ' @tanstack/vue-query'
84
85
const route = useRoute (' /users/[id]' )
85
86
86
87
// const { data: user, isLoading, error } = useUserData()
@@ -89,10 +90,14 @@ const {
89
90
error : tqError,
90
91
status,
91
92
fetchStatus,
93
+ refetch,
92
94
} = useQuery ({
93
95
async queryFn() {
94
- console .log (' [TQ]useUserData' , route .fullPath )
96
+ console .log (' [TQ]useUserData' , route .fullPath , route . params )
95
97
await delay (500 )
98
+ if (route .params .id === ' 0' ) {
99
+ throw new Error (' no user' )
100
+ }
96
101
const user = {
97
102
id: route .params .id ,
98
103
// @ts-expect-error: no param "name"!
@@ -102,9 +107,61 @@ const {
102
107
return user
103
108
},
104
109
// FIXME: (to) => ['user-id', to.params.id]
105
- queryKey: [' users' , () => route .params .id ],
110
+ queryKey: [' users' , route .params .id ],
106
111
staleTime: 5000 ,
112
+ retry: 0 ,
113
+ })
114
+
115
+ const {
116
+ mutateAsync : mutate,
117
+ data,
118
+ context,
119
+ status : mutationStatus,
120
+ error,
121
+ } = useMutation ({
122
+ mutationFn : async (id : number ) => {
123
+ console .log (' mutate' , id )
124
+ await delay (id )
125
+ // throw new Error('data ' + id)
126
+ return id
127
+ },
128
+ mutationKey: [
129
+ ' mutate' ,
130
+ () => {
131
+ console .log (' mutationKey' )
132
+ return 205
133
+ },
134
+ ],
135
+ // mutationKey: () => {
136
+ // console.log('mutationKey')
137
+ // return ['mutate']
138
+ // },
139
+ onMutate : (id ) => {
140
+ console .log (' onMutate' , id )
141
+ // throw new Error('hello')
142
+ return { id }
143
+ },
144
+ onSuccess : (newData , vars , context ) => {
145
+ console .log (' onSuccess' , newData , data .value , vars , context )
146
+ // throw new Error('onSuccess')
147
+ },
148
+ onError : (err ) => {
149
+ console .log (' onError' , err )
150
+ // throw new Error('onError')
151
+ },
152
+ async onSettled(data , error ) {
153
+ await new Promise ((r ) => setTimeout (r , 100 ))
154
+ console .log (' onSettled' , data , error )
155
+ throw new Error (' onSettled' )
156
+ },
157
+ retry: false ,
107
158
})
159
+
160
+ function multipleMutate() {
161
+ mutate (100 ).then ((d ) => console .log (' DATA 100' , d ))
162
+ mutate (200 ).then ((d ) => console .log (' DATA 200' , d ))
163
+ mutate (50 ).then ((d ) => console .log (' DATA 50' , d ))
164
+ }
108
165
</script >
109
166
110
167
<template >
@@ -119,22 +176,44 @@ const {
119
176
<RouterLink :to =" { params: { id: Number(route.params.id) + 1 } }"
120
177
>Next</RouterLink
121
178
>
179
+ |
180
+ <button
181
+ @click ="
182
+ refetch().then((d) => {
183
+ console.log('Got ', d)
184
+ })
185
+ "
186
+ >
187
+ Refresh
188
+ </button >
122
189
123
- <h2 >Data Loaders</h2 >
190
+ <!-- < h2>Data Loaders</h2>
124
191
<pre v-if="isLoading">Loading...</pre>
125
192
<pre v-else-if="error">Error: {{ error }}</pre>
126
193
<pre v-else>{{ user }}</pre>
127
194
128
195
<hr />
129
196
130
- <h2 >TQ</h2 >
197
+ -->
198
+
199
+ <h2 >Query</h2 >
131
200
132
201
<p >
133
202
<code >status: {{ status }}</code >
134
203
<br />
135
204
<code >fetchStatus: {{ fetchStatus }}</code >
136
205
</p >
137
- <pre v-if =" tqError" >Error: {{ tqError }}</pre >
138
- <pre v-else >{{ tqUser == null ? String(tqUser) : tqUser }}</pre >
206
+ <pre >Error: {{ tqError }}</pre >
207
+ <pre >User: {{ tqUser == null ? String(tqUser) : tqUser }}</pre >
208
+
209
+ <hr />
210
+
211
+ <h2 >Mutations</h2 >
212
+
213
+ <pre >Context: {{ context }}</pre >
214
+ <pre >status: {{ mutationStatus }}</pre >
215
+ <pre >error: {{ error }}</pre >
216
+ <button @click =" mutate(123)" >Mutate</button >
217
+ <button @click =" multipleMutate()" >Multi Mutate</button >
139
218
</main >
140
219
</template >
0 commit comments