使用Nuxt拦截apollo模块上的网络错误

问题描述 投票:1回答:1

我正在使用nuxtapollo-module我需要拦截可能的网络错误(401/403更具体)所以我可以显示一些错误模式并注销我的用户。在文档中,我看到在nuxt.config.js内你可以这样做:

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

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

vue.js apollo nuxt.js apollo-client vue-apollo
1个回答
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'
    },
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.