Vue Query is a library designed to simplify data fetching, caching, and synchronization for Vue applications, bringing the powerful features of TanStack Query (formerly React Query) to the Vue ecosystem. It provides a set of hooks for declarative and efficient management of asynchronous data, reducing boilerplate and improving developer experience. The current stable version is 1.26.0, but version 2.x is under active beta development, which tightly aligns its core logic with `@tanstack/query-core`. This alignment in v2 is a key differentiator, ensuring consistency with the broader TanStack Query ecosystem. It addresses common challenges like caching, background refetching, optimistic updates, and pagination out-of-the-box, making it an advanced solution compared to manual `fetch` or `axios` implementations. The library is actively maintained with ongoing feature development and bug fixes.
npm install vue-queryVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `vue-query` in a Vue 3 application. It initializes a `QueryClient`, provides it via `QueryClientProvider`, and then uses `useQuery` within a component to fetch and display data from a public API, handling loading and error states.
Review the official migration guide for `@tanstack/query` and `vue-query` v2 when it becomes stable. Specifically, check for changes related to query keys, query options, and client configuration.
Ensure your project is configured for ESM by using `"type": "module"` in your `package.json` or by configuring your bundler (e.g., Webpack, Rollup, Vite) to correctly handle ESM imports and exports for `vue-query`.
For Vue 2, install `npm install @vue/composition-api` and then `Vue.use(VueCompositionAPI)` in your main.js. For Nuxt 2, install `@nuxtjs/composition-api` and ensure it's configured in `nuxt.config.js`.
Ensure that `useQuery` and other hooks are invoked directly within the `setup()` function of a Vue component or within a custom composable function that is itself called from `setup()`.
Ensure `import { QueryClientProvider } from 'vue-query';` is used and that you are rendering it correctly in your template or render function, e.g., `<QueryClientProvider :client="queryClient">` or `h(QueryClientProvider, { client: queryClient }, ...)`. Remember it's a named export.Wrap your root Vue component (or the relevant part of your app) with `<QueryClientProvider :client="queryClient">` and ensure `queryClient` is an instance of `new QueryClient()`.
Verify that `useQuery`, `useMutation`, etc., are only called directly within the `setup()` method of your components or within custom composable functions that are themselves called from `setup()`.