|
1 | | -# ⚗️ My Module Nuxt |
| 1 | +# ⚗️ Vue Query Nuxt |
2 | 2 |
|
3 | | -[](https://github.com/Hebilicious/authjs-nuxt/actions/workflows/ci.yaml) |
4 | | -[](https://badge.fury.io/js/@hebilicious%2Fauthjs-nuxt) |
| 3 | +[](https://github.com/Hebilicious/vue-query-nuxt/actions/workflows/ci.yaml) |
| 4 | +[](https://badge.fury.io/js/@hebilicious%2Fvue-query-nuxt) |
5 | 5 | [](https://opensource.org/licenses/MIT) |
6 | 6 |
|
7 | | -🚀 Welcome to __Hebilicious Nuxt Module Starter Template__! |
| 7 | +🚀 Welcome to __Vue Query Nuxt__! |
8 | 8 |
|
| 9 | +This Nuxt Module automatically installs and configure Vue Query for your Nuxt application. |
| 10 | +It has 0 config out-of-the box and extremely lightweight. |
9 | 11 |
|
10 | 12 | ## ⚠️ Disclaimer |
11 | 13 |
|
12 | | -_🧪 This module is really unstable and is not recommended for production use. It is intended for those who want to experiment with the edge._ |
| 14 | +_🧪 This module is in active development._ |
13 | 15 |
|
| 16 | +Refer to the [Vue Query documentation](https://tanstack.com/query/latest/docs/vue/quick-start) for more information about Vue Query. |
14 | 17 |
|
15 | 18 | ## 📦 Installation |
16 | 19 |
|
17 | 20 |
|
| 21 | +1. Use npm, pnpm or yarn to install the dependencies. |
| 22 | + |
| 23 | +```bash |
| 24 | +npm i @hebilicious/vue-query-nuxt @tanstack/vue-query |
| 25 | +``` |
| 26 | + |
18 | 27 | ```bash |
19 | | -npm i |
| 28 | +pnpm i @hebilicious/vue-query-nuxt @tanstack/vue-query |
| 29 | +``` |
| 30 | + |
| 31 | +```bash |
| 32 | +yarn i @hebilicious/vue-query-nuxt @tanstack/vue-query |
| 33 | +``` |
| 34 | + |
| 35 | +2. Add the modules to your Nuxt modules |
| 36 | + |
| 37 | +```ts |
| 38 | +// In nuxt.config.ts |
| 39 | +export default defineNuxtConfig({ |
| 40 | + modules: ["@hebilicious/vue-query-nuxt"], |
| 41 | + // These are the default values, you do not need to specify them. |
| 42 | + // Refer to the vue-query docs for more information. |
| 43 | + vueQuery: { |
| 44 | + stateKey: "vue-query-nuxt", |
| 45 | + queryClientOptions: { |
| 46 | + defaultOptions: { queries: { staleTime: 5000 } } // default |
| 47 | + }, |
| 48 | + vueQueryPluginOptions: {} |
| 49 | + } |
| 50 | +}) |
| 51 | +``` |
| 52 | + |
| 53 | +3. Use right away |
| 54 | + |
| 55 | +```html |
| 56 | +<script setup> |
| 57 | +import { useQueryClient, useQuery, useMutation } from '@tanstack/vue-query' |
| 58 | +
|
| 59 | +// Access QueryClient instance |
| 60 | +const queryClient = useQueryClient() |
| 61 | +
|
| 62 | +// Define a fetching function |
| 63 | +const getTodos = () => $fetch("/api/todos") |
| 64 | +
|
| 65 | +// Query |
| 66 | +const { isLoading, isError, data, error } = useQuery({ |
| 67 | + queryKey: ['todos'], |
| 68 | + queryFn: getTodos, |
| 69 | +}) |
| 70 | +
|
| 71 | +// Mutation |
| 72 | +const mutation = useMutation({ |
| 73 | + mutationFn: postTodo, |
| 74 | + onSuccess: () => { |
| 75 | + // Invalidate and refetch |
| 76 | + queryClient.invalidateQueries({ queryKey: ['todos'] }) |
| 77 | + }, |
| 78 | +}) |
| 79 | +
|
| 80 | +function onButtonClick() { |
| 81 | + mutation.mutate({ |
| 82 | + id: Date.now(), |
| 83 | + title: 'Do Laundry', |
| 84 | + }) |
| 85 | +} |
| 86 | +</script> |
| 87 | + |
| 88 | +<template> |
| 89 | + <span v-if="isLoading">Loading...</span> |
| 90 | + <span v-else-if="isError">Error: {{ error.message }}</span> |
| 91 | + <!-- We can assume by this point that `isSuccess === true` --> |
| 92 | + <ul v-else> |
| 93 | + <li v-for="todo in data" :key="todo.id">{{ todo.title }}</li> |
| 94 | + </ul> |
| 95 | + <button @click="onButtonClick">Add Todo</button> |
| 96 | +</template> |
| 97 | +``` |
| 98 | + |
| 99 | +4. Advanced configuration |
| 100 | + |
| 101 | +Create a `vue-query.config.ts` file at the root of your project. |
| 102 | + |
| 103 | +```ts |
| 104 | +// vue-query.config.ts |
| 105 | +import { library } from "@example/libray" |
| 106 | + |
| 107 | +export default defineVueQueryPluginCallback((vueQueryOptions) => { |
| 108 | + console.log(vueQueryOptions) // You can access the queryClient here |
| 109 | + return { provide: { library, test: console } } |
| 110 | +}) |
20 | 111 | ``` |
21 | 112 |
|
| 113 | +This callback will be run *directly* after the Vue Query plugin is installed, so you can use it to provide something here. |
| 114 | +This can be useful if you want to configure something that needs the queryClient or you want to provide a library. |
22 | 115 |
|
23 | 116 | ## 📦 Contributing |
24 | 117 |
|
|
0 commit comments