@@ -37,16 +37,7 @@ yarn i @hebilicious/vue-query-nuxt @tanstack/vue-query
3737``` ts
3838// In nuxt.config.ts
3939export 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- }
40+ modules: [" @hebilicious/vue-query-nuxt" ]
5041})
5142```
5243
@@ -59,26 +50,23 @@ import { useQueryClient, useQuery, useMutation } from '@tanstack/vue-query'
5950// Access QueryClient instance
6051const queryClient = useQueryClient ()
6152
62- // Define a fetching function
63- const getTodos = () => $fetch (" /api/todos" )
64-
6553// Query
6654const { isLoading , isError , data , error } = useQuery ({
6755 queryKey: [' todos' ],
68- queryFn: getTodos,
56+ queryFn : () => $fetch ( " /api/todos " ), // Use $fetch with your api routes to get typesafety
6957})
7058
7159// Mutation
72- const mutation = useMutation ({
73- mutationFn: postTodo,
60+ const { mutate } = useMutation ({
61+ mutationFn : ( newTodo ) => $fetch ( " /api/todos " , { method : " POST " , body : newTodo })
7462 onSuccess : () => {
7563 // Invalidate and refetch
7664 queryClient .invalidateQueries ({ queryKey: [' todos' ] })
7765 },
7866})
7967
8068function onButtonClick () {
81- mutation . mutate ({
69+ mutate ({
8270 id: Date .now (),
8371 title: ' Do Laundry' ,
8472 })
@@ -98,7 +86,23 @@ function onButtonClick() {
9886
99874 . Advanced configuration
10088
101- Create a ` vue-query.config.ts ` file at the root of your project.
89+ You can specify the options under the vueQuery key in your nuxt.config.ts file.
90+ Everything is typed.
91+
92+ ``` ts
93+ // In nuxt.config.ts
94+ export default defineNuxtConfig ({
95+ modules: [" @hebilicious/vue-query-nuxt" ],
96+ vueQuery: {
97+ stateKey: " vue-query-nuxt" ,
98+ queryClientOptions: {
99+ defaultOptions: { queries: { staleTime: 5000 } } // default
100+ },
101+ vueQueryPluginOptions: {}
102+ }
103+ })
104+ ```
105+ If you need to modify the plugin that installs vue query, you can create a ` vue-query.config.ts ` file at the root of your project.
102106
103107``` ts
104108// vue-query.config.ts
@@ -110,8 +114,8 @@ export default defineVueQueryPluginCallback((vueQueryOptions) => {
110114})
111115```
112116
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.
117+ This callback will be run * directly* after the Vue Query plugin is installed, so you can use it to ` provide ` something.
118+ This can be useful if you want to configure something that needs the ` queryClient ` or you want to provide a library in the same plugin .
115119
116120## 📦 Contributing
117121
0 commit comments