Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A lightweight, 0 config Nuxt Module for Vue Query.

License

NotificationsYou must be signed in to change notification settings

Hebilicious/vue-query-nuxt

Repository files navigation

CInpm versionnpm downloadsLicense: MIT

🚀 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.

Features

  • 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.

📦 How to use

1. Use npm, pnpm or yarn to install the dependencies.

npx nuxi@latest module add vue-querynpm i @tanstack/vue-query

2. Add the modules to your Nuxt modules

Innuxt.config.ts :

exportdefaultdefineNuxtConfig({modules:["@hebilicious/vue-query-nuxt"]})

3. Use right away

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>

4. Advanced configuration

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.

📦 Contributing

Contributions, issues and feature requests are welcome!

  1. Fork this repo

  2. Installnode andpnpmUsecorepack enable && corepack prepare pnpm@latest --activate to install pnpm easily

  3. Usepnpm i at the mono-repo root.

  4. Make modifications and follow conventional commits.

  5. Open a PR 🚀🚀🚀

About

A lightweight, 0 config Nuxt Module for Vue Query.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors4

  •  
  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp