vue 全局引入axios

1.安装
网上有多种方法,这里使用npm的安装方式:

npm install axios

出现如下表示安装完成:
vue 全局引入axios
2.全局注册axios

main.js
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

//在main.js加上以下两句
import axios from 'axios'  //引入axios
Vue.prototype.axios = axios  //全局注册,任何组件都能直接使用

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

到这里就引入完毕了

3.组件中使用

在任意组件,直接使用this.axios	
<template>
    <div>
        <button @click="btn">点击获取数据</button>
    </div>
</template>
<script>
export default {
    methods:{
        btn(){
            this.axios.get('https://easy-mock.com/mock/5bff9732ec952807e818415e/admin/team')
            .then((response) => {
                console.log(response);
            })
            .catch((error) => {
                console.log(error);
            });
        }
    }
}
</script>

打印结果:

vue 全局引入axios