基本使用

  1. npm install axios --save
  2. import axios from 'axios'
  3. Vue.config.productionTip = false
  4. new Vue({
  5. render: h => h(App),
  6. }).$mount('#app')
  7. axios({
  8. //123.207.32.32:8000/home/multidata
  9. url:'http://123.207.32.32:8000/home/multidata',
  10. method:'get',
  11. params:{}//get传参
  12. data:{}//post传参
  13. }).then(res=>{
  14. console.log(res);
  15. });
  16. axios.get("url",{参数}).then();
  17. axios.post("url",data:{参数}).then();

发送并发请求

  1. 使用axios.all, 可以放入多个请求的数组.
  2. axios.all([]) 返回的结果是一个数组,使用 axios.spread() 可将数组 [res1,res2] 展开为 res1, res2
  3. axios.all([
  4. axios.get('http://123.207.32.32:8000/home/multidata'),
  5. axios.get('http://123.207.32.32:8000/home/multidata')
  6. ]).then(result=>{
  7. //result返回的是一个数组结果存在了数组里面
  8. console.log(result);
  9. });
  10. axios.all([
  11. axios.get('http://123.207.32.32:8000/home/multidata'),
  12. axios.get('http://123.207.32.32:8000/home/multidata')
  13. ]).then(axios.spread((参数1,参数2...)=>{}));//有多个请求设置多少个参数会把返回的数组自动展开

全局配置

  1. axios.defaults.属性名 可设置对应属性的默认值 在后面的axios中就可以了省略
  2. axios.defaults.baseURL='http://123.207.32.32:8000'
  3. axios({
  4. url:'/home/multidata',
  5. method:'get'
  6. }).then(res=>{
  7. console.log(res);
  8. });
  1. 属性配置
  2. 请求地址
  3. url: '/user',
  4. 请求类型
  5. method: 'get', -->'post' 'put' 'delete'
  6. 请根路径
  7. baseURL: 'http://www.mt.com/api',
  8. 自定义的请求头
  9. headers:{'x-Requested-With':'XMLHttpRequest'},
  10. URL get传参
  11. params:{ id: 12 },
  12. post 传参
  13. data: { key: 'aa'},
  14. auth:{} oauth2设置安全信息
  15. 超时设置s
  16. timeout: 1000,
  17. 响应的数据格式 "" 默认类型 DOMString,与设置为 text 相同
  18. json 是一个json格式的字符串
  19. blob 是一个包含二进制数据的 Blob 对象
  20. document 是一个 HTML Document
  21. arraybuffer 是一个包含二进制数据的流
  22. text 文本
  23. responseType: 'json',

通过实例的方式请求

  1. //由于通过 axios直接请求 设置全局配置ip 等无法改变 可通过创建实例的方式单独设置
  2. const instance = axios.create({
  3. baseURL:"http://123.207.32.32:8000",
  4. timeout:5000
  5. });
  6. instance({
  7. url:'/home/data',
  8. params:{type:'pop',page:'1'},
  9. method:'get'
  10. }).then(res=>{
  11. console.log(res);
  12. });

axios的封装

  1. 在每个组件中发送网络请求时都需要通过import axios from 'axios'去重新引用
  1. import axios from 'axios'
  2. export default function request(config){
  3. const instance = axios.create({
  4. baseURL:"http://123.207.32.32:8000",
  5. timeout:5000
  6. });
  7. //发送真正的网络请求
  8. return instance(config);
  9. }
  10. 使用 组件创建时请求
  11. created(){
  12. request({//传递的请求信息
  13. url:'/home/data',
  14. params:{type:'pop',page:'1'},
  15. method:'get'
  16. }.then(sussece=>{
  17. //then 和 catch 执行成功或者失败的内容
  18. suc(sussece);
  19. }).catch(error=>{
  20. err(error);
  21. })
  22. }

拦截器

  1. instance.interceptors.request.use(succ=>{
  2. //请求的全部信息
  3. console.log(succ);
  4. //放行
  5. return succ
  6. },error=>{
  7. //出现错误信息
  8. console.log(error);
  9. })
  10. //响应时的拦截器
  11. instance.interceptors.response.use(succ=>{
  12. return succ
  13. },error=>{
  14. //出现错误信息
  15. })
  1. 可以做的事情
  2. 比如 config中的信息不符合服务要求进行拦截处理
  3. 比如每次请求时 可以在页面显示一个加载图标
  4. 某些网络请求 必须携带特殊信息 不携带跳转到其他位置

文件下载问题

  1. import axios from "axios";
  2. const service = axios.create({
  3. responseType: 'arraybuffer' //设置响应的数据类型 下载文件要设置成arraybuffer 或者blob
  4. });
  5. service.interceptors.request.use(config => {
  6. let item = window.sessionStorage.getItem('tokenStorage');
  7. if (item) {
  8. //向请求头中加入携带信息
  9. config.headers['Authorization'] = item;
  10. }
  11. return config;
  12. },error => {
  13. if (error.response.code == 403){
  14. console.log("1")
  15. }
  16. });
  17. //在响应拦截器中
  18. service.interceptors.response.use(config => {
  19. //获取响应结果中的数据 通过Blob对象转换成二进制文件流
  20. const blog = new Blob([config.data]);
  21. //将二进制数据流生成一个本地的URL
  22. const href = window.URL.createObjectURL(blog)
  23. //创建一个a标签
  24. let a = document.createElement('a')
  25. //设置路径
  26. a.href = href
  27. //设置下载文件的名字 -- 如果没有会在页面中显示
  28. a.download = "员工表.xls"
  29. //加入到Body中
  30. document.body.appendChild(a)
  31. //点击--会自动下载下来
  32. a.click()
  33. //删除本地的URL
  34. window.URL.revokeObjectURL(href)
  35. //删除a标签
  36. document.body.removeChild(a)
  37. });
  38. export const downloadUser = (url, parms) => {
  39. return service({
  40. method: 'get',
  41. url,
  42. data: parms
  43. });

使用

  1. downloadUser('http://localhost:8081/employee/basic/downloadUser');