带有nuxt-axios的多个API提供程序和一个用于默认标头的插件

vue.js Vue.js

宝儿

2020-04-07

我有一个Nuxt应用程序,它对同一API的请求很多,但是我还需要向除我的主要API之外的其他提供程序发出请求,而且我不知道如何管理默认标头。

这是我的工作设置,创建一个插件以将标头添加到所有请求中,如下所示:

plugins/axios.js

export default function({ $axios, store, redirect }) {
   $axios.onRequest(config => {
       config.headers.common.Authorization = 'token 123';
       config.headers.common["Custom-header"] = 'blablabla';
 }
}

nuxt.config.js

module.exports = {
    plugins: ["@/plugins/axios"],
    axios: {
        baseURL: process.env.API_URL,
    }
}

store.js

async changeKeyVersionOnline({ commit }) {
    const response = await this.$axios.get(
      `users/1`
    );
    return response;
  },

这对于主要API来说效果很好,但问题是我还需要向第三方服务提供商的其他端点发出请求,并且标头应该不同。

How can i do that, i read about the proxy option of the nuxt-axios package but what i understand is this only changes the request base URL, i cant find how to set different headers to a specific request.

第4127篇《带有nuxt-axios的多个API提供程序和一个用于默认标头的插件》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

0个回答

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android