Axios是一个基于 promise的http库,用于node.js和浏览器中
Axios的下载
npm安装
npm install axios
cdn
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
vue中使用axios
在main.js中引用
import axios from 'axios'
把axios挂载到vue上,$http这个名称可以自定义
Vue.prototype.$http = axios
axios的具体使用
get请求
this.$http.get('需要请求的接口')
.then((res)=>{
console.log(res);//成功的信息
).catch((err)=>{
console.log(err);//错误的信息
)
post请求
//data请求数据
this.$http.post('需要请求的接口', {data})
.then((res)=>{
console.log(res);//成功的信息
).catch((err)=>{
console.log(err);//错误的信息
)