Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8
A lightweight, 0 config Nuxt Module for Vue Query.
License
Hebilicious/vue-query-nuxt
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
🚀 Welcome toVue Query Nuxt!
This Nuxt Module automatically installs and configure Vue Query for your Nuxt application.It has 0 config out-of-the box and extremely lightweight.
- 0 config out-of-the box
- All configurations options available
- Auto Imports for Vue Query composables
Refer to theVue Query documentation for more information about Vue Query.
npx nuxi@latest module add vue-querynpm i @tanstack/vue-query
Innuxt.config.ts
:
exportdefaultdefineNuxtConfig({modules:["@hebilicious/vue-query-nuxt"]})
In a vue component :
<scriptsetuplang="ts">// Access QueryClient instanceconstqueryClient=useQueryClient()// Queryconst{ isLoading, isError, data, suspense, error}=useQuery({queryKey:['todos'],queryFn:()=>$fetch("/api/todos"),// Use $fetch with your api routes to get typesafety})//SuspenseonServerPrefetch(suspense)// Mutationconst{ mutate}=useMutation({mutationFn:(newTodo)=>$fetch("/api/todos",{method:"POST",body:newTodo})onSuccess:()=>{// Invalidate and refetchqueryClient.invalidateQueries({queryKey:['todos']})},})functiononButtonClick(){mutate({id:Date.now(),title:'Do Laundry',})}</script><template><spanv-if="isLoading">Loading...</span><spanv-else-if="isError">Error: {{ error.message }}</span><!-- We can assume by this point that `isSuccess === true` --><ulv-else><liv-for="todo in data":key="todo.id">{{ todo.title }}</li></ul><button@click="onButtonClick">Add Todo</button></template>
You can specify the options under the vueQuery key in your nuxt.config.ts file.Everything is typed.
Innuxt.config.ts
:
exportdefaultdefineNuxtConfig({modules:["@hebilicious/vue-query-nuxt"],vueQuery:{// useState key used by nuxt for the vue query state.stateKey:"vue-query-nuxt",// default// If you only want to import some functions, specify them here.// You can pass false or an empty array to disable this feature.// default: ["useQuery", "useQueries", "useInfiniteQuery", "useMutation", "useIsFetching", "useIsMutating", "useQueryClient"]autoImports:["useQuery"],// Pass the vue query client options here ...queryClientOptions:{defaultOptions:{queries:{staleTime:5000}}// default},// Pass the vue query plugin options here ....vueQueryPluginOptions:{enableDevtoolsV6Plugin:true,// enable integrate with the official vue devtools}}})
If you need to modify the plugin that installs vue query, you can create avue-query.config.ts
file at the root of your project.
Invue-query.config.ts
:
import{library}from"@example/libray"exportdefaultdefineVueQueryPluginHook(({ queryClient, nuxt})=>{console.log(queryClient,nuxt)// You can access the queryClient herereturn{pluginReturn:{provide:{ library,test:console}},// nuxt plugin return valuevueQueryPluginOptions:{ queryClient}// You can pass dynamic options}})
This hook will be run within the nuxt plugin installed by the module, so you can use it toprovide
something or replace the vue query options.This can be useful if you need to run custom logic when thequeryClient
is being installed.
Contributions, issues and feature requests are welcome!
Fork this repo
Install
node
andpnpm
Usecorepack enable && corepack prepare pnpm@latest --activate
to install pnpm easilyUse
pnpm i
at the mono-repo root.Make modifications and follow conventional commits.
Open a PR 🚀🚀🚀
About
A lightweight, 0 config Nuxt Module for Vue Query.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.