use axios globally in all my components vue

前端 未结 1 353
轮回少年
轮回少年 2020-12-01 07:54

I am testing with axios within a Vue application and the CLI. I\'ve been using vue-resource and I could access it on all my components by simply passing it to Vue.use (VueRe

相关标签:
1条回答
  • 2020-12-01 08:28

    In main.js you can just assign Axios to $http.

    main.js

    import Axios from 'axios'
    
    Vue.prototype.$http = Axios;
    

    By modifying the vue prototype, any vue instance will have the ability to call $http on this. (e.g. this.$http.get('https://httpbin.org/get')

    Note: $http is the axios object now, so any method you can call on axios object, you can call on this.$http.

    0 讨论(0)
提交回复
热议问题