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

README.md

117 lines3.3 KB

1<div align="center">
2 <picture>
3 <source
4 media="(prefers-color-scheme: dark)"
5 srcset="https://tanstack.com/api/readme/query.png?framework=vue&theme=dark"
6 />
7 <source
8 media="(prefers-color-scheme: light)"
9 srcset="https://tanstack.com/api/readme/query.png?framework=vue"
10 />
11 <img
12 src="https://tanstack.com/api/readme/query.png?framework=vue"
13 alt="TanStack Vue Query"
14 width="900"
15 />
16 </picture>
17</div>
18[![Vue Query logo](https://raw.githubusercontent.com/TanStack/query/main/packages/vue-query/media/vue-query.png)](https://github.com/TanStack/query/tree/main/packages/vue-query)
19​
20[![npm version](https://img.shields.io/npm/v/@tanstack/vue-query)](https://www.npmjs.com/package/@tanstack/vue-query)
21[![npm license](https://img.shields.io/npm/l/@tanstack/vue-query)](https://github.com/TanStack/query/blob/main/LICENSE)
22[![bundle size](https://img.shields.io/bundlephobia/minzip/@tanstack/vue-query)](https://bundlephobia.com/package/@tanstack/vue-query)
23[![npm](https://img.shields.io/npm/dm/@tanstack/vue-query)](https://www.npmjs.com/package/@tanstack/vue-query)
24​
25# Vue Query
26​
27Hooks for fetching, caching and updating asynchronous data in Vue.
28​
29Support for Vue 2.x via [vue-demi](https://github.com/vueuse/vue-demi)
30​
31# Documentation
32​
33Visit https://tanstack.com/query/latest/docs/vue/overview
34​
35# Quick Features
36​
37- Transport/protocol/backend agnostic data fetching (REST, GraphQL, promises, whatever!)
38- Auto Caching + Refetching (stale-while-revalidate, Window Refocus, Polling/Realtime)
39- Parallel + Dependent Queries
40- Mutations + Reactive Query Refetching
41- Multi-layer Cache + Automatic Garbage Collection
42- Paginated + Cursor-based Queries
43- Load-More + Infinite Scroll Queries w/ Scroll Recovery
44- Request Cancellation
45- (experimental) [Suspense](https://v3.vuejs.org/guide/migration/suspense.html#introduction) + Fetch-As-You-Render Query Prefetching
46- (experimental) SSR support
47- Dedicated Devtools
48- [![npm bundle size](https://img.shields.io/bundlephobia/minzip/@tanstack/vue-query)](https://bundlephobia.com/package/@tanstack/vue-query) (depending on features imported)
49​
50# Quick Start
51​
521. Install `vue-query`
53​
54 ```bash
55 $ npm i @tanstack/vue-query
56 ```
57​
58 or
59​
60 ```bash
61 $ pnpm add @tanstack/vue-query
62 ```
63​
64 or
65​
66 ```bash
67 $ yarn add @tanstack/vue-query
68 ```
69​
70 or
71​
72 ```bash
73 $ bun add @tanstack/vue-query
74 ```
75​
76 > If you are using Vue 2.6, make sure to also setup [@vue/composition-api](https://github.com/vuejs/composition-api)
77​
782. Initialize **Vue Query** via **VueQueryPlugin**
79​
80 ```tsx
81 import { VueQueryPlugin } from '@tanstack/vue-query'
82 import { createApp } from 'vue'
83 import App from './App.vue'
84​
85 createApp(App).use(VueQueryPlugin).mount('#app')
86 ```
87​
883. Use query
89​
90 ```tsx
91 import { useQuery } from '@tanstack/vue-query'
92 import { defineComponent } from 'vue'
93​
94 export default defineComponent({
95 name: 'MyComponent',
96 setup() {
97 const query = useQuery({ queryKey: ['todos'], queryFn: getTodos })
98​
99 return {
100 query,
101 }
102 },
103 })
104 ```
105​
1064. If you need to update options on your query dynamically, make sure to pass them as reactive variables
107​
108 ```tsx
109 const id = ref(1)
110 const enabled = ref(false)
111​
112 const query = useQuery({
113 queryKey: ['todos', id],
114 queryFn: () => getTodos(id),
115 enabled,
116 })
117 ```