参考文章 https://www.jianshu.com/p/523b0690a003
1、安装
npm install axios --save
2、引入
在main.js文件中引入(在main.js引入即为全局引入)
// axios需要使用prototype将axios挂载到原型上 ,$后面是自己另起的名称,以后就可以使用该名称
Vue.prototype.$axios = Axios
3、使用
方式一
// 使用方法一
this.$axios.get()
this.$axios.post()
方式二
// 使用方法二
this.$axios({
method: 'get', // 方法
url: url, // 地址
data: { //参数
xxx = xxx
}
})
4、具体使用
get方法无参数时:
// get方法无参数时
this.$axios.get(url)
.then(res => {
})
.catch(error => {
console.log(error)
})
get方法有参数时方法一:
// get方法有参数时方法一
this.$axios.get("url?xxx=xxx")
.then(res => {
})
.catch(error => {
console.log(error)
})
get方法有参数时方法二:
// get方法有参数时方法二
this.$axios.get(url,{
params:{
xxx = xxx
}
})
.then(res => {
})
.catch(error => {
console.log(error)
})
5、全局默认设置
在main.js文件中设置
// 全局默认设置
Axios.defaults.baseURL = 'url'
// 还可设置请求头headers等