Django 这个.$apollo总是没有定义

Django 这个.$apollo总是没有定义,django,vue.js,graphql,apollo,Django,Vue.js,Graphql,Apollo,我正在尝试使用apollo+vue,django,但由于某些原因,它无法在组件中加载/使用。apollo始终是未定义的 阿波罗 import Vue from 'vue' import VueApollo from 'vue-apollo' import { createApolloClient, restartWebsockets } from 'vue-cli-plugin-apollo/graphql-client' // Install the vue plugin Vue.use(V

我正在尝试使用apollo+vue,django,但由于某些原因,它无法在组件中加载/使用。apollo始终是未定义的

阿波罗

import Vue from 'vue'
import VueApollo from 'vue-apollo'
import { createApolloClient, restartWebsockets } from 'vue-cli-plugin-apollo/graphql-client'

// Install the vue plugin
Vue.use(VueApollo)

// Name of the localStorage item
const AUTH_TOKEN = 'jwt-token'

// Config
const defaultOptions = {
  httpEndpoint: '/graphql',
  wsEndpoint: null,
  tokenName: AUTH_TOKEN,
  persisting: false,
  websocketsOnly: false,
  ssr: false
}

// Call this in the Vue app file
export function createProvider (options = {}) {
  // Create apollo client
  const { apolloClient, wsClient } = createApolloClient({
    ...defaultOptions,
    ...options
  })
  apolloClient.wsClient = wsClient

  // Create vue apollo provider
  const apolloProvider = new VueApollo({
    defaultClient: apolloClient,
    defaultOptions: {
      $query: {
        loadingKey: 'loading',
        fetchPolicy: 'cache-and-network'
      }
    },
    errorHandler (error) {
      // eslint-disable-next-line no-console
      console.log('%cError', 'background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold;', error.message)
    }
  })

  return apolloProvider
}

// Manually call this when user log in
export async function onLogin (apolloClient, token) {
  localStorage.setItem(AUTH_TOKEN, token)
  if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
  try {
    await apolloClient.resetStore()
  } catch (e) {
    // eslint-disable-next-line no-console
    console.log('%cError on cache reset (login)', 'color: orange;', e.message)
  }
}

// Manually call this when user log out
export async function onLogout (apolloClient) {
  localStorage.removeItem(AUTH_TOKEN)
  if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
  try {
    await apolloClient.resetStore()
  } catch (e) {
    // eslint-disable-next-line no-console
    console.log('%cError on cache reset (logout)', 'color: orange;', e.message)
  }
}
为什么阿波罗号从未上膛?配置中缺少什么

[编辑:遵循教程时也会发生同样的情况:]

import Vue from "vue"
import App from "./App.vue"
import { router } from './routes.js'
import ApolloClient from "apollo-boost"
import VueApollo from "vue-apollo"

const apolloProvider = new VueApollo({
  defaultClient: new ApolloClient({
    uri: "http://localhost:8000/graphql/"
  })
})


Vue.use(VueApollo)
new Vue({
  router,
  el: "#app",
  provide: apolloProvider.provide(),
  render: h => h(App)
})

如果您使用VUE cli>3创建VUE项目,这将导致此问题。$apollo等于未定义。

如果您使用VUE cli>3创建VUE项目,这将导致此问题。$apollo等于未定义。

@Daniel,我添加了apollo.js配置。但我也像在文档中一样尝试了,甚至使用了boost快捷方式,结果都是一样的。@DanielRearden是的。你可以看到它就在那里,对吗?那么它提供了什么呢?这是我在不同教程中发现的众多变体之一,因为官方文档中的版本也不起作用。例如,它以同样的方式失败:@Daniel,我添加了apollo.js配置。但我也像在文档中一样尝试了,甚至使用了boost快捷方式,结果都是一样的。@DanielRearden是的。你可以看到它就在那里,对吗?那么它提供了什么呢?这是我在不同教程中发现的众多变体之一,因为官方文档中的版本也不起作用。例如,这同样失败:简单地说明问题的根源通常远不如提出解决方案有用。请考虑为您的答案添加一个潜在的解决方案。如果没有答案,你的答案更适合作为对原始问题的评论。简单地说明问题的根源通常远不如提出解决方案有用。请考虑为您的答案添加一个潜在的解决方案。如果没有,你的答案更适合作为对原始问题的评论。
import Vue from 'vue'
import VueApollo from 'vue-apollo'
import { createApolloClient, restartWebsockets } from 'vue-cli-plugin-apollo/graphql-client'

// Install the vue plugin
Vue.use(VueApollo)

// Name of the localStorage item
const AUTH_TOKEN = 'jwt-token'

// Config
const defaultOptions = {
  httpEndpoint: '/graphql',
  wsEndpoint: null,
  tokenName: AUTH_TOKEN,
  persisting: false,
  websocketsOnly: false,
  ssr: false
}

// Call this in the Vue app file
export function createProvider (options = {}) {
  // Create apollo client
  const { apolloClient, wsClient } = createApolloClient({
    ...defaultOptions,
    ...options
  })
  apolloClient.wsClient = wsClient

  // Create vue apollo provider
  const apolloProvider = new VueApollo({
    defaultClient: apolloClient,
    defaultOptions: {
      $query: {
        loadingKey: 'loading',
        fetchPolicy: 'cache-and-network'
      }
    },
    errorHandler (error) {
      // eslint-disable-next-line no-console
      console.log('%cError', 'background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold;', error.message)
    }
  })

  return apolloProvider
}

// Manually call this when user log in
export async function onLogin (apolloClient, token) {
  localStorage.setItem(AUTH_TOKEN, token)
  if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
  try {
    await apolloClient.resetStore()
  } catch (e) {
    // eslint-disable-next-line no-console
    console.log('%cError on cache reset (login)', 'color: orange;', e.message)
  }
}

// Manually call this when user log out
export async function onLogout (apolloClient) {
  localStorage.removeItem(AUTH_TOKEN)
  if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient)
  try {
    await apolloClient.resetStore()
  } catch (e) {
    // eslint-disable-next-line no-console
    console.log('%cError on cache reset (logout)', 'color: orange;', e.message)
  }
}
import Vue from "vue"
import App from "./App.vue"
import { router } from './routes.js'
import ApolloClient from "apollo-boost"
import VueApollo from "vue-apollo"

const apolloProvider = new VueApollo({
  defaultClient: new ApolloClient({
    uri: "http://localhost:8000/graphql/"
  })
})


Vue.use(VueApollo)
new Vue({
  router,
  el: "#app",
  provide: apolloProvider.provide(),
  render: h => h(App)
})