|
1 | 1 | --- |
2 | | -title: Vue Query (Coming Soon) |
| 2 | +title: Vue Query |
3 | 3 | --- |
4 | 4 |
|
5 | | -> ⚠️ This module has not yet been developed. It requires an adapter similar to `react-query` to work. We estimate the amount of code to do this is low-to-moderate, but does require familiarity with the Vue framework. If you would like to contribute this adapter, please open a PR! |
| 5 | +The `vue-query` package offers a 1st-class API for using TanStack Query via Vue. However, all of the primitives you receive from these hooks are core APIs that are shared across all of the TanStack Adapters including the Query Client, query results, query subscriptions, etc. |
6 | 6 |
|
7 | | -The `@tanstack/vue-query` package offers a 1st-class API for using TanStack Query via Vue. However, all of the primitives you receive from this API are core APIs that are shared across all of the TanStack Adapters including the Query Client, query results, query subscriptions, etc. |
| 7 | +## Example |
| 8 | + |
| 9 | +This example very briefly illustrates the 3 core concepts of Vue Query: |
| 10 | + |
| 11 | +- [Queries](guides/queries) |
| 12 | +- [Mutations](guides/mutations) |
| 13 | +- [Query Invalidation](guides/query-invalidation) |
| 14 | + |
| 15 | +```vue |
| 16 | +<script setup> |
| 17 | +import { useQueryClient, useQuery, useMutation } from "vue-query"; |
| 18 | +
|
| 19 | +// Access QueryClient instance |
| 20 | +const queryClient = useQueryClient(); |
| 21 | +
|
| 22 | +// Query |
| 23 | +const { isLoading, isError, data, error } = useQuery(["todos"], getTodos); |
| 24 | +
|
| 25 | +// Mutation |
| 26 | +const mutation = useMutation(postTodo, { |
| 27 | + onSuccess: () => { |
| 28 | + // Invalidate and refetch |
| 29 | + queryClient.invalidateQueries(["todos"]); |
| 30 | + }, |
| 31 | +}); |
| 32 | +
|
| 33 | +function onButtonClick() { |
| 34 | + mutation.mutate({ |
| 35 | + id: Date.now(), |
| 36 | + title: "Do Laundry", |
| 37 | + }); |
| 38 | +} |
| 39 | +</script> |
| 40 | +
|
| 41 | +<template> |
| 42 | + <span v-if="isLoading">Loading...</span> |
| 43 | + <span v-else-if="isError">Error: {{ error.message }}</span> |
| 44 | + <!-- We can assume by this point that `isSuccess === true` --> |
| 45 | + <ul v-else> |
| 46 | + <li v-for="todo in data" :key="todo.id">{{ todo.title }}</li> |
| 47 | + </ul> |
| 48 | + <button @click="onButtonClick">Add Todo</button> |
| 49 | +</template> |
| 50 | +``` |
| 51 | + |
| 52 | +These three concepts make up most of the core functionality of Vue Query. The next sections of the documentation will go over each of these core concepts in great detail. |
0 commit comments