基本使用
npm install axios --saveimport axios from 'axios'Vue.config.productionTip = falsenew Vue({render: h => h(App),}).$mount('#app')axios({//123.207.32.32:8000/home/multidataurl:'http://123.207.32.32:8000/home/multidata',method:'get',params:{}//get传参data:{}//post传参}).then(res=>{console.log(res);});axios.get("url",{参数}).then();axios.post("url",data:{参数}).then();
发送并发请求
使用axios.all, 可以放入多个请求的数组.axios.all([]) 返回的结果是一个数组,使用 axios.spread() 可将数组 [res1,res2] 展开为 res1, res2axios.all([axios.get('http://123.207.32.32:8000/home/multidata'),axios.get('http://123.207.32.32:8000/home/multidata')]).then(result=>{//result返回的是一个数组结果存在了数组里面console.log(result);});axios.all([axios.get('http://123.207.32.32:8000/home/multidata'),axios.get('http://123.207.32.32:8000/home/multidata')]).then(axios.spread((参数1,参数2...)=>{}));//有多个请求设置多少个参数会把返回的数组自动展开
全局配置
axios.defaults.属性名 可设置对应属性的默认值 在后面的axios中就可以了省略axios.defaults.baseURL='http://123.207.32.32:8000'axios({url:'/home/multidata',method:'get'}).then(res=>{console.log(res);});
属性配置请求地址url: '/user',请求类型method: 'get', -->'post' 'put' 'delete'请根路径baseURL: 'http://www.mt.com/api',自定义的请求头headers:{'x-Requested-With':'XMLHttpRequest'},URL get传参params:{ id: 12 },post 传参data: { key: 'aa'},auth:{} oauth2设置安全信息超时设置stimeout: 1000,响应的数据格式 "" 默认类型 DOMString,与设置为 text 相同json 是一个json格式的字符串blob 是一个包含二进制数据的 Blob 对象document 是一个 HTML Documentarraybuffer 是一个包含二进制数据的流text 文本responseType: 'json',
通过实例的方式请求
//由于通过 axios直接请求 设置全局配置ip 等无法改变 可通过创建实例的方式单独设置const instance = axios.create({baseURL:"http://123.207.32.32:8000",timeout:5000});instance({url:'/home/data',params:{type:'pop',page:'1'},method:'get'}).then(res=>{console.log(res);});
axios的封装
在每个组件中发送网络请求时都需要通过import axios from 'axios'去重新引用
import axios from 'axios'export default function request(config){const instance = axios.create({baseURL:"http://123.207.32.32:8000",timeout:5000});//发送真正的网络请求return instance(config);}使用 组件创建时请求created(){request({//传递的请求信息url:'/home/data',params:{type:'pop',page:'1'},method:'get'}.then(sussece=>{//then 和 catch 执行成功或者失败的内容suc(sussece);}).catch(error=>{err(error);})}
拦截器
instance.interceptors.request.use(succ=>{//请求的全部信息console.log(succ);//放行return succ},error=>{//出现错误信息console.log(error);})//响应时的拦截器instance.interceptors.response.use(succ=>{return succ},error=>{//出现错误信息})
可以做的事情比如 config中的信息不符合服务要求进行拦截处理比如每次请求时 可以在页面显示一个加载图标某些网络请求 必须携带特殊信息 不携带跳转到其他位置
文件下载问题
import axios from "axios";const service = axios.create({responseType: 'arraybuffer' //设置响应的数据类型 下载文件要设置成arraybuffer 或者blob});service.interceptors.request.use(config => {let item = window.sessionStorage.getItem('tokenStorage');if (item) {//向请求头中加入携带信息config.headers['Authorization'] = item;}return config;},error => {if (error.response.code == 403){console.log("1")}});//在响应拦截器中service.interceptors.response.use(config => {//获取响应结果中的数据 通过Blob对象转换成二进制文件流const blog = new Blob([config.data]);//将二进制数据流生成一个本地的URLconst href = window.URL.createObjectURL(blog)//创建一个a标签let a = document.createElement('a')//设置路径a.href = href//设置下载文件的名字 -- 如果没有会在页面中显示a.download = "员工表.xls"//加入到Body中document.body.appendChild(a)//点击--会自动下载下来a.click()//删除本地的URLwindow.URL.revokeObjectURL(href)//删除a标签document.body.removeChild(a)});export const downloadUser = (url, parms) => {return service({method: 'get',url,data: parms});
使用
downloadUser('http://localhost:8081/employee/basic/downloadUser');
