透過 100 多個技巧的集合來學習 Nuxt!

vue-query
@hebilicious/vue-query-nuxt

用於 @tanstack/vue-query 的零配置輕量級 Nuxt 模組。

⚗️ Vue Query Nuxt

CInpm versionnpm downloadsLicense: MIT

🚀 歡迎使用 Vue Query Nuxt

這個 Nuxt 模組會自動為您的 Nuxt 應用程式安裝並設定 Vue Query。它開箱即用,零配置且極其輕巧。

功能

  • 開箱即用,零配置
  • 所有配置選項均可用
  • 自動導入 Vue Query 的 composables

有關 Vue Query 的更多資訊,請參考 Vue Query 文件

📦 如何使用

1. 使用 npm、pnpm 或 yarn 安裝依賴項。

# npm
npm i @hebilicious/vue-query-nuxt @tanstack/vue-query  
# pnpm
pnpm i @hebilicious/vue-query-nuxt @tanstack/vue-query  
# yarn
yarn add @hebilicious/vue-query-nuxt @tanstack/vue-query

2. 將模組添加到您的 Nuxt 模組

nuxt.config.ts

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

3. 立即使用

在 vue 組件中

<script setup lang="ts">
// Access QueryClient instance
const queryClient = useQueryClient()

// Query
const { isLoading, isError, data, error } = useQuery({
  queryKey: ['todos'],
  queryFn: () => $fetch("/api/todos"), // Use $fetch with your api routes to get typesafety 
})

// Mutation
const { mutate } = useMutation({
  mutationFn: (newTodo) => $fetch("/api/todos", { method: "POST", body: newTodo })
  onSuccess: () => {
    // Invalidate and refetch
    queryClient.invalidateQueries({ queryKey: ['todos'] })
  },
})

function onButtonClick() {
   mutate({
    id: Date.now(),
    title: 'Do Laundry',
  })
}
</script>

<template>
  <span v-if="isLoading">Loading...</span>
  <span v-else-if="isError">Error: {{ error.message }}</span>
  <!-- We can assume by this point that `isSuccess === true` -->
  <ul v-else>
    <li v-for="todo in data" :key="todo.id">{{ todo.title }}</li>
  </ul>
  <button @click="onButtonClick">Add Todo</button>
</template>

4. 進階配置

您可以在 `nuxt.config.ts` 檔案中的 `vueQuery` 鍵下指定選項。所有內容都有類型定義。

nuxt.config.ts

export default defineNuxtConfig({
  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: {}
  }
})

如果您需要修改安裝 vue query 的插件,可以在專案的根目錄建立一個 vue-query.config.ts 檔案。

vue-query.config.ts

import { library } from "@example/libray"

export default defineVueQueryPluginHook(({ queryClient, nuxt }) => {
  console.log(queryClient, nuxt) // You can access the queryClient here
  return {
    pluginReturn: { provide: { library, test: console } }, // nuxt plugin return value
    vueQueryPluginOptions: { queryClient } // You can pass dynamic options
  }
})

此 hook 將在模組安裝的 nuxt 插件中執行,因此您可以使用它來 provide 某些東西或替換 vue query 選項。如果需要在安裝 queryClient 時執行自訂邏輯,這會很有用。

📦 貢獻

歡迎貢獻、提交問題和功能請求!

  1. Fork 這個 repo
  2. 安裝 nodepnpm使用 corepack enable && corepack prepare pnpm@latest --activate 輕鬆安裝 pnpm
  3. 在 mono-repo 根目錄使用 pnpm i
  4. 進行修改並遵循 conventional commits。
  5. 開啟 PR 🚀🚀🚀