搜尋

首頁  >  問答  >  主體

使用Nuxt Composition API和Nuxt Apollo模組的技巧和指南

<p>為什麼我可以在nuxt組合API中使用nuxt apollo模組?如果我嘗試使用這個外掛範例:</p> <pre class="brush:php;toolbar:false;">import {Context} from '@nuxt/types' import { provide, onGlobalSetup, defineNuxtPlugin } from '@nuxtjs/composition-api' import {DefaultApolloClient} from '@vue/apollo-composable/dist' /*** 這個插件將連接@nuxt/apollojs和@vue/apollo-composable*/ export default defineNuxtPlugin(({app}: Context): void => { onGlobalSetup(() => { provide(DefaultApolloClient, app.apolloProvider?.defaultClient) }) })</pre> <p>我得到了這個錯誤:<code>Error: Apollo client with id default not found. Use provideApolloClient() if you are outside of a component setup</code></p>
P粉134288794P粉134288794482 天前599

全部回覆(1)我來回復

  • P粉399585024

    P粉3995850242023-08-27 00:00:55

    1. 安裝 vue/apollo-composable: npm install --save @vue/apollo-composable

    2. 建立 nuxt 外掛程式(provide-apollo-client.ts):

    import { Context } from '@nuxt/types'
    import {
      onGlobalSetup,
      defineNuxtPlugin
    } from '@nuxtjs/composition-api'
    // @ts-ignore
    import { provideApolloClient } from '@vue/apollo-composable'
    
    /**
     * This plugin will connect @nuxt/apollojs with @vue/apollo-composable
     */
    
    export default defineNuxtPlugin(({ app }: Context): void => {
      onGlobalSetup(() => {
        provideApolloClient(app.apolloProvider?.defaultClient)
      })
    })

    回覆
    0
  • 取消回覆