1、作用:
axios是一个基于promise的HTTP库,其本质也是对原生XHR的封装,用来向后端发送http请求,获取后端发送的数据
2、用法:
2.1 安装:
npm install --save axios
2.2 导入
在想要使用的组件内,引入axios。
import axios from "axios"
2.3 get请求
你的方法(){
let url = "https://xxx/xx"
axios.get(url).then(
//成功
(response) => {
//获取到的数据,具体结构具体分析
console.log(response.data.result);
},
//失败
(error) => {
console.log('出错了:',error)
}
)
}
2.4 post请求
你的方法(){
let apiKey = 'xxxx'
let url = 'https://api.xxx/getXxx'
this.axios.post(url, `apiKey=${apiKey}`).then(
//成功
(response) => {
//获取到的数据,具体结构具体分析
console.log(response.data.result);
},
//失败
(error) => {
console.log('出错了:',error)
}
)
}