Search code examples
vue.jsaxiosnuxt.js

NuxtJs axios module global config


I need to set global configuration for @nuxt/axios.

For example I need default Content-Type, Accept, Authorization header etc. and other configurations for all of my API calls. Is there any way to add these settings globally in nuxt to avoid repetition of code.


Solution

  • As stated here: https://axios.nuxtjs.org/extend
    You can create a plugin and dump your axios configuration there like this nuxt.config.js

    export default {
      plugins: [
        '~/plugins/axios'
      ]
    }
    

    /plugins/axios.js

    export default ({ $axios, $config: { baseUrlIam, secretToken } }) => {
      $axios.defaults.baseURL = baseUrlIam
      $axios.defaults.headers.Authorization = secretToken
    }
    

    Also, setHeader may be useful.