使用 nuxt 组合 api 和 nuxt appollo 模块

Usage nuxt composition api and nuxt appollo module

为什么我可以使用nuxt apollo 模块和nuxt 组合API?如果我尝试使用这个插件示例:

import {Context} from '@nuxt/types'
import {
  provide,
  onGlobalSetup,
  defineNuxtPlugin
} from '@nuxtjs/composition-api'
import {DefaultApolloClient} from '@vue/apollo-composable/dist'

/**
 * This plugin will connect @nuxt/apollojs with @vue/apollo-composable
 */

export default defineNuxtPlugin(({app}: Context): void => {
  onGlobalSetup(() => {
    provide(DefaultApolloClient, app.apolloProvider?.defaultClient)
  })
})

我明白了Error: Apollo client with id default not found. Use provideApolloClient() if you are outside of a component setup

  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)
  })
})