博客
关于我
Vue中axios的使用
阅读量:266 次
发布时间:2019-03-01

本文共 1255 字,大约阅读时间需要 4 分钟。

Axios框架的基本使用

1. Axios框架的安装与使用

首先需要安装axios包,并在项目中导入使用。安装命令为:

npm install axios

然后,在你的项目中导入:

import axios from 'axios'

接着,在Vue应用中配置生产提醒为false:

Vue.config.productionTip = false

创建Vue实例并挂载到 DOM 中:

new Vue({  store,  render: h => h(App)}).$mount('#app')

2. Axios发送并发请求

通过 axios.all() 方法可以同时发送多个请求:

axios.all([  axios({    url: 'http://123.207.32.32:8000/home/multidata'  }),  axios({    url: 'http://123.207.32.32:8000/home/data',    params: {      type: 'sell',      page: 5    }  })])  .then((res) => {    console.log(res);    console.log(res[0]);    console.log(res[1]);  })

3. Axios的配置信息

在项目中可以对axios的默认配置进行设置,例如设置全局的baseURL:

axios.defaults.baseURL = 'http://123.207.32.32:8000'

4. Axios拦截器的使用

可以通过拦截器来对请求或响应进行处理。例如,创建一个带有拦截器的axios实例:

const instance = axios.create({  baseURL: 'http://123.207.32.32:8000',  timeout: 5000})instance.interceptors.request.use((config) => {  // 配置请求拦截  return config})instance.interceptors.response.use((res) => {  // 配置响应拦截  return res})instance(config)  .then((res) => {    // 处理成功回调  })  .catch((err) => {    // 处理错误回调  })

5. 其他常用操作

可以通过解构赋值的方式快速获取对象的某些属性:

const obj = {  name: 'kobe',  age: 12}const { name, age } = obj

对于数组进行解构赋值:

const names = ['qqq', 'www', 'eee']const [name1, name2, name3] = names

通过上述方法,可以方便地处理数据和请求。

转载地址:http://zfox.baihongyu.com/

你可能感兴趣的文章
Openlayers实战:绘制图形,导出KML文件
查看>>
Openlayers实战:绘制多边形,导出CSV文件
查看>>
Openlayers实战:绘制带箭头的线
查看>>
Openlayers实战:输入WKT数据,输出GML、Polyline、GeoJSON格式数据
查看>>
Openlayers高级交互(10/20):绘制矩形,截取对应部分的地图并保存
查看>>
Openlayers高级交互(11/20):显示带箭头的线段轨迹,箭头居中
查看>>
Openlayers高级交互(14/20):汽车移动轨迹动画(开始、暂停、结束)
查看>>
Openlayers高级交互(15/20):显示海量多边形,10ms加载完成
查看>>
Openlayers高级交互(16/20):两个多边形的交集、差集、并集处理
查看>>
Openlayers高级交互(17/20):通过坐标显示多边形,计算出最大幅宽
查看>>
Openlayers高级交互(19/20): 地图上点击某处,列表中显示对应位置
查看>>
Openlayers高级交互(2/20):清除所有图层的有效方法
查看>>
Openlayers高级交互(20/20):超级数据聚合,页面不再混乱
查看>>
Openlayers高级交互(3/20):动态添加 layer 到 layerGroup,并动态删除
查看>>
Openlayers高级交互(6/20):绘制某点,判断它是否在一个电子围栏内
查看>>
Openlayers高级交互(7/20):点击某点弹出窗口,自动播放视频
查看>>
Openlayers高级交互(8/20):选取feature,平移feature
查看>>
Openlayers:DMS-DD坐标形式互相转换
查看>>
openlayers:圆孔相机根据卫星经度、纬度、高度、半径比例推算绘制地面的拍摄的区域
查看>>
OpenLDAP(2.4.3x)服务器搭建及配置说明
查看>>