javascript - add axios authorization on nuxt config - Stack Overflow

How to add a global authorization header on nuxt.config.js?triedaxios: {defaults : {headers : {mon: [{

How to add a global authorization header on nuxt.config.js?

tried

  axios: {
    defaults : {
      headers : {
        mon: [
          {
            'Authorization' : '5fb9c42ceba425fb9c42ceba43'
          }
        ]
      }
    }
  },

but not working

I can do

this.$axios.setHeader('Authorization', this.$store.state.appstore.akey);

but I find it not ideal when having multiple axios request because I have to add it on every request

How to add a global authorization header on nuxt.config.js?

tried

  axios: {
    defaults : {
      headers : {
        mon: [
          {
            'Authorization' : '5fb9c42ceba425fb9c42ceba43'
          }
        ]
      }
    }
  },

but not working

I can do

this.$axios.setHeader('Authorization', this.$store.state.appstore.akey);

but I find it not ideal when having multiple axios request because I have to add it on every request

Share Improve this question asked Nov 22, 2020 at 4:52 Juliver GalletoJuliver Galleto 9,05727 gold badges92 silver badges169 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Try this, taken from the module docs.

axios: {
  headers : {
    mon: {
      'Authorization' : '5fb9c42ceba425fb9c42ceba43'
    }
  }
}

I have created an plugin for it. Goto plugins and create axios.js:

export default function ({ $axios, store }) {
  if (process.client) {
    $axios.setToken(store.state.appstore.akey, 'Bearer')
  }
}

Then register your plugin in nuxt.config.js

plugins: ['@/plugins/axios'],

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745530075a4631653.html

相关推荐

  • javascript - add axios authorization on nuxt config - Stack Overflow

    How to add a global authorization header on nuxt.config.js?triedaxios: {defaults : {headers : {mon: [{

    23小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信