使用Nuxt拦截阿波罗模块上的网络错误

时间:2019-04-01 06:20:44

标签: vue.js apollo nuxt.js apollo-client vue-apollo

我将nuxtapollo-module一起使用,我需要拦截可能的网络错误(更具体地讲是401/403),以便显示一些错误模式并注销用户。在文档中,我看到在nuxt.config.js内您可以执行以下操作:

  apollo: {
    tokenName: 'Authorization',
    authenticationType: 'Bearer',
    errorHandler(error) { do something }
  }
...

但是在该配置文件中,我无法访问所需的应用程序功能(例如,错误模式或路由器)。有什么办法可以存档吗?

1 个答案:

答案 0 :(得分:1)

您可以使用apollo-error-link

  apollo: {
    clientConfigs: {
      default: '~/apollox/client-configs/default.js'
    }
  },

在这里配置

import { onError } from 'apollo-link-error'

export default function(ctx) {
  const errorLink = onError(({ graphQLErrors, networkError }) => {

  })
  return {
    link: errorLink,

    // required
    httpEndpoint: ctx.app.$env.GRAPHQL_URL,

    httpLinkOptions: {
      credentials: 'same-origin'
    },
  }
}
相关问题