如何让apollo在vue组件外部可访问.

我正在验证用户是否存在,然后允许路由继续.

{
        path: '/:username',
        name: 'userProfilePage',
        component: userProfilePage,
        beforeEnter(routeTo, routeFrom, next) {
            userExist(routeTo.params.username)
            next()
        }

username作为参数传递给userExist函数.

import gql from "graphql-tag"

export default function userExist(username) {
    this.$apollo
        .query({
            query: gql`
        query($username: String!) {
            login(username: $username) {
                username
                email
            }
        }
    `,
            variables: {
                username: username
            }
        })
        .then(res => {
            console.log(res);
            return res
        })
        .catch(err => {
            console.log(err);
            return err
        });
}

但它正在输出错误:

enter image description here

阿波罗客户端代码

import Vue from 'vue'
import App from './App.vue'
import VueApollo from 'vue-apollo';
import { ApolloClient } from 'apollo-client'
import { HttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import router from './routes.js'

Vue.config.productionTip = false

const httpLink = new HttpLink({
    uri: process.env.VUE_APP_DB_URL,
})

const cache = new InMemoryCache()

const apolloClient = new ApolloClient({
    link: httpLink,
    cache
})

Vue.use(VueApollo)

const apolloProvider = new VueApollo({
    defaultClient: apolloClient,
})

new Vue({
    render: h => h(App),
    router,
    apolloProvider
}).$mount('#app')

推荐答案

因此,不要在应用程序中初始化apollo客户端.vue文件,在另一个文件中初始化它.

const httpLink = new HttpLink({
    uri: process.env.VUE_APP_DB_URL,
})

const cache = new InMemoryCache()

export const apolloClient = new ApolloClient({
    link: httpLink,
    cache
})

完成后,在应用程序中导入.vue文件如下:

import { apolloClient } from './clients.js';

Vue.use(VueApollo)

const apolloProvider = new VueApollo({
    defaultClient: apolloClient,
})

new Vue({
    render: h => h(App),
    router,
    apolloProvider
}).$mount('#app')

完成后,将该客户机导入到您想要的任何其他文件中:

import { apolloClient } from './client.js';
import gql from "graphql-tag"

export default function userExist(username) {
apolloClient
    .query({
        query: gql`
    query($username: String!) {
        login(username: $username) {
            username
            email
        }
    }
`,
        variables: {
            username: username
        }
    })
    .then(res => {
        console.log(res);
        return res
    })
    .catch(err => {
        console.log(err);
        return err
    });
}

Vue.js相关问答推荐

虚拟DOM在Vue中姗姗来迟

如何使用 vueJS 上的 ref( ) 函数创建二维数组定义?

为什么 Pinia/Vuex 比使用服务类的classic 方法更受欢迎?

如何在Typescript语言Vuejs中使用watch功能?

Vue.js 如何计算总数?

使用 vee-validate 在 vue js 中进行验证有错误

如何获取 Vue 路由历史记录?

在组件的样式部分使用 Vue 变量

Vue @click 不适用于存在 href 的anchor锚标记

VueJS 组件中的图像未加载

即使在客户端设置 Access-Control-Allow-Origin 或其他 Access-Control-Allow-* 标头后也出现 CORS 错误

如何在 vue 3 中获取路由的参数?

npm run dev 和 npm run production 的区别

如何在两个属性上使用 Vue/Vuetify 自动完成过滤器?

异步api调用后如何使用vuex getter

如何从组件内的单点捕获 vuejs 错误

如何设置 :id 前缀字符串?

VS Code - 在 .vue 文件中启用 autoClosingTags 选项

Vue 3 组合 API 数据()函数

Vue.js 显示空格(换行符)