Wenn Sie aus irgendeinem Grund wünschen, dass Sie einfach die gleiche Abfragefunktion für Ihre gesamte App freigeben und nur Abfrageschlüssel verwenden könnten, um zu identifizieren, was abgerufen werden soll, können Sie dies tun, indem Sie eine Standard-Abfragefunktion für TanStack Query bereitstellen.
// Define a default query function that will receive the query key
const defaultQueryFn = async ({ queryKey }) => {
const { data } = await axios.get(
`https://jsonplaceholder.typicode.com${queryKey[0]}`,
)
return data
}
// provide the default query function to your app with defaultOptions
const vueQueryPluginOptions: VueQueryPluginOptions = {
queryClientConfig: {
defaultOptions: { queries: { queryFn: defaultQueryFn } },
},
}
app.use(VueQueryPlugin, vueQueryPluginOptions)
// All you have to do now is pass a key!
const { status, data, error, isFetching } = useQuery({
queryKey: [`/posts/${postId}`],
})
// Define a default query function that will receive the query key
const defaultQueryFn = async ({ queryKey }) => {
const { data } = await axios.get(
`https://jsonplaceholder.typicode.com${queryKey[0]}`,
)
return data
}
// provide the default query function to your app with defaultOptions
const vueQueryPluginOptions: VueQueryPluginOptions = {
queryClientConfig: {
defaultOptions: { queries: { queryFn: defaultQueryFn } },
},
}
app.use(VueQueryPlugin, vueQueryPluginOptions)
// All you have to do now is pass a key!
const { status, data, error, isFetching } = useQuery({
queryKey: [`/posts/${postId}`],
})
Wenn Sie jemals die Standard-QueryFn überschreiben möchten, können Sie Ihre eigene wie gewohnt bereitstellen.