Vue3(撩课学院)笔记09-axios简介,发起get请求的两种方式,发起带参的get及post请求,发起并发请求,并发请求结果将数组展开,axios全局配置,axios配置及封装,请求和响应拦截

本文详细介绍了axios在网络请求中的应用,包括发起get和post请求的两种方式,带参数的请求,并发请求的实现,全局配置,以及如何进行请求和响应的拦截。此外,还展示了如何对axios进行封装,创建一个通用的http服务,并在实际应用中使用这个封装后的服务进行并发请求。
摘要由CSDN通过智能技术生成
1、axios简介

axios是基于promise可以用于浏览器和node.js的网络请求库,在服务器端使用原生node.js,在浏览气短使用ajax(即XMLHttpRequests)

2、axios发起get请求

撩课提供的api接口地址:
http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001

<template>
  <div>
    <h1>axios-发起get请求</h1>
    <button @click="getReq()">发起get请求</button>
  </div>
</template>

<script >
  import axios from  'axios'
export  default {
  setup(){
    const getReq=()=>{
      //发起get请求
      axios.get('http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001').then((res)=>{
          console.log(res.data)
        }
      ).catch((err)=>{
        console.log(err)
      })
    }

    return {
      getReq,
    }
  }
}
</script>

<style>
/*引入样式*/

</style>

3、axios发起get请求的第二种方法

使用axios进行封装

<template>
  <div>
    <h1>axios-发起get请求</h1>
    <button @click="getReq()">发起get请求</button>
  </div>
</template>

<script >
  import axios from  'axios'
export  default {
  setup(){
    const getReq=()=>{
      axios({
        method:'get',
	url:'http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001',

      }).then((res)=>{
        console.log(res.data)
      }).catch((err)=>{
        console.log(err)
      })
    }

    return {
      getReq,
    }
  }
}
</script>

<style>
/*引入样式*/

</style>
4、axios发起带参数的get请求,及带参的post请求

要看注释

<template>
  <div>
    <h1>axios-发起get请求</h1>
    <button @click="getReq()">发起get请求</button>
    <h1>axios-发起post请求</h1>
    <button @click="postReq()">发起get请求</button>
  </div>
</template>

<script >
  import axios from  'axios'
export  default {
  setup(){
    const getReq=()=>{
      //发起get请求
      axios.get(
        'http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001',
        {
          params: {
            id:'lk001',
            name:'like',
            age:10
          }
        }
      ).then((res)=>{
          console.log(res.data)
        }
      ).catch((err)=>{
        console.log(err)
      })

      // axios({
      //   method:'get',
      //   url:'http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001',
      //   //传入参数
      //     params:{
      //       id:'lk001',
      //       name:'like',
      //       age:10
      //     }
      //
      // }).then((res)=>{
      //   console.log(res.data)
      // }).catch((err)=>{
      //   console.log(err)
      // })
    }
    const postReq=()=>{
      //发起post请求
      // axios.post(
      //   'http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001',
      //   {//post请求的参数使用data
      //     data: {
      //       id:'lk001',
      //       name:'like',
      //       age:10
      //     }
      //   }
      // ).then((res)=>{
      //     console.log(res.data)
      //   }
      // ).catch((err)=>{
      //   console.log(err)
      // })

      //使用post请求传参
      axios({
        method:'post',
        url:'http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001',
        //传入参数,post请求使用data
          data:{
            id:'lk001',
            name:'like',
            age:10
          }

      }).then((res)=>{
        console.log(res.data)
      }).catch((err)=>{
        console.log(err)
      })

    }
    return {
      getReq,
      postReq
    }
  }
}
</script>

<style>
/*引入样式*/

</style>
5、axios.all发起并发请求

使用axios.all发起并发请求,并通过axios.spread将数组的值展开
测试api接口,共两个
http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001
http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk002

请求是放在数组里的

<template>
  <div>
    <h1>axios-发起并发请求</h1>
    <button @click="allReq()">发起并发请求</button>
  </div>
</template>

<script >
  import axios from  'axios'
export  default {
  setup(){

    const allReq=()=>{
    //请求是放在数组里的
      axios.all([
        //get请求
        axios({url:'http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001'}),
        //另一个get请求
        axios({url:'http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk002'}),
      ]).then((res)=>{
        console.log(res)
      }).catch((err)=>{
        console.log(err)
      })
    }
    return {
      allReq
    }
  }
}
</script>

<style>
/*引入样式*/

</style>
6、axios.all发起并发请求并通过axios.spread将数组的值展开
<template>
  <div>
    <h1>axios-发起并发请求</h1>
    <button @click="allReq()">发起并发请求</button>
  </div>
</template>

<script >
  import axios from  'axios'
export  default {
  setup(){

    const allReq=()=>{
      axios.all([
        //get请求
        axios({url:'http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001'}),
        //另一个get请求
        axios({url:'http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk002'}),
      ]).then(//使用axios.spread展开结果集数组,spread里的回调函数里面有两个参数
          axios.spread((res1,res2)=>{
            console.log(res1.data)
            console.log(res2.data)
          })
      ).catch((err)=>{
        console.log(err)
      })
    }
    return {
      allReq
    }
  }
}
</script>

<style>
/*引入样式*/

</style>
7、axios配置
<template>
  <div>
    <h1>axios-发起并发请求</h1>
    <button @click="allReq()">发起并发请求</button>
  </div>
</template>

<script >
  import axios from  'axios'
export  default {
  setup(){
    //axios的全局配置
    axios.defaults.baseURL='http://demo.itlike.com/web/xlmc/api/'
    axios.defaults.timeout=5000//毫秒
    const allReq=()=>{
      axios.all([
        //get请求
        axios({url:'homeApi/categoriesdetail/lk001'}),
        //另一个get请求
        axios({url:'homeApi/categoriesdetail/lk002'}),
      ]).then(//使用axios.spread展开结果集数组,spread里的回调函数里面有两个参数
          axios.spread((res1,res2)=>{
            console.log(res1.data)
            console.log(res2.data)
          })
      ).catch((err)=>{
        console.log(err)
      })
    }
    return {
      allReq
    }
  }
}
</script>

<style>
/*引入样式*/

</style>

8、axios封装

创建封装文件:src/http/index.js
src/http/index.js

import axios from 'axios'
//axios的全局配置
axios.defaults.baseURL='http://demo.itlike.com/web/xlmc/api/'
axios.defaults.timeout=5000//毫秒
//包装一个函数
export  default function ajax(url='',params={},type='get') {
  return new Promise((resolve,reject)=>{//promise已经调用立即执行
    //1、创建一个变量
    let promise
    //2、判断请求类型
    if (type.toUpperCase()==='GET'){//如果是get请求
      promise=axios({
        url,
        params,
      })
    }else if(type.toUpperCase()==='POST'){//如果是post请求
      promise=axios({
        url,
        data:params,
        method:'POST',
      })
    }

    //3、处理返回
    promise.then((res)=>{
      resolve(res)
    }).catch((err)=>{
      reject(err)
    })
  })
}

App.vue

<template>
  <div>
    <h1>axios-发起get请求</h1>
    <button @click="getReq()">发起get请求</button>
    <h1>axios-发起带参get请求</h1>
    <button @click="getReqParam()">发起带参get请求</button>
    <h1>axios-发起post请求</h1>
    <button @click="postReq()">发起post带参请求</button>
    <h1>axios-发起并发请求</h1>
    <button @click="postReqall()">发起并发请求</button>
  </div>
</template>

<script >
import ajax from  './http/index.js'
export  default {
  setup(){
    //发起get请求
    const getReq=()=>{
      ajax('http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001').then((res)=>{
        console.log(res.data)
      }).catch((err)=>{
        console.log(err)
      })

    }
    const getReqParam=()=>{
      //发起带参get请求
      ajax(
        'http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001',
        {
          id:'lk001',
          name:'like',
          age:10
        }
      ).then((res)=>{
        console.log(res.data)
      }).catch((err)=>{
        console.log(err)
      })
    }

    //使用post请求传参
    const postReq=()=>{
      ajax(
        'http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001',
        {
          id:'lk001',
          name:'like',
          age:10
        },
        'POST'
      ).then((res)=>{
        console.log(res.data)
      }).catch((err)=>{
        console.log(err)
      })

    }

    const postReqall=()=>{
      Promise.all([
        ajax('http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk001'),
        ajax('http://demo.itlike.com/web/xlmc/api/homeApi/categoriesdetail/lk002')
      ]).then((res)=>{
        console.log(res)
      })

    }

    return {
      getReq,
      getReqParam,
      postReq,
      postReqall
    }

  }
}
</script>

<style>
/*引入样式*/

</style>

9、请求和响应拦截

在src/http/index.js封装文件上添加请求和响应拦截配置

import axios from 'axios'
//axios的全局配置
axios.defaults.baseURL='http://demo.itlike.com/web/xlmc/api/'
axios.defaults.timeout=5000//毫秒

//请求拦截
//axios配置拦截
axios.interceptors.request.use(
  //拦截成功
  (config)=>{
    //在请求中添加一些数据
    config.abc='hello world'
    //console.log(config)
  //如果不拦截,就要再返回出去这个config
    return config
  },
  //拦截失败
  (err)=>{
    return Promise.error(err)
  }
)

//响应拦截
//axios配置拦截
axios.interceptors.response.use(
  //拦截成功
  (response)=>{
    console.log(response)
    //如果不拦截,就要再返回出去这个config
    return response.data
  },
  //拦截失败
  (err)=>{
    return Promise.error(err)
  }
)


//包装一个函数
export  default function ajax(url='',params={},type='get') {
  return new Promise((resolve,reject)=>{//promise已经调用立即执行
    //1、创建一个变量
    let promise
    //2、判断请求类型
    if (type.toUpperCase()==='GET'){//如果是get请求
      promise=axios({
        url,
        params,
      })
    }else if(type.toUpperCase()==='POST'){//如果是post请求
      promise=axios({
        url,
        data:params,
        method:'POST',
      })
    }

    //3、处理返回
    promise.then((res)=>{
      resolve(res)
    }).catch((err)=>{
      reject(err)
    })
  })
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值