<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          axios的簡(jiǎn)單封裝

          共 3453字,需瀏覽 7分鐘

           ·

          2021-03-09 19:52

          html中簡(jiǎn)單封裝axios

          /**
          ?*?@author?[Moisten]?itcode.icu
          ?*?@version?1.0.0
          ?*?@overview?封裝axios請(qǐng)求
          ?*?
          ?*?調(diào)用方法:
          ?*?var?datas?=?values;
          ?*?axiosPostRequst(`register`,?datas).then(res?=>?{?console.log('post請(qǐng)求--->',?res)?});
          ?*?
          ?*/

          //?默認(rèn)環(huán)境配置
          axios.defaults.baseURL?=?'http://192.168.0.102/Home/Index/'

          //?請(qǐng)求超時(shí)設(shè)置
          axios.defaults.timeout?=?50000

          //?post請(qǐng)求頭
          axios.defaults.headers.post['Content-Type']?=?'application/x-www-form-urlencoded;charset=UTF-8'

          //?請(qǐng)求攔截器
          axios.interceptors.request.use(config?=>?{
          ????return?config
          })


          //?post請(qǐng)求
          function?axiosPostRequst(url,?data)?{
          ????return?new?Promise((resolve,?reject)?=>?{
          ????????axios
          ????????????.post(url,?data)
          ????????????.then(res?=>?{
          ????????????????resolve(res.data)
          ????????????})
          ????????????.catch(err?=>?{
          ????????????????reject(err.data)
          ????????????})
          ????})
          }

          //?get請(qǐng)求
          function?axiosGetRequst(url)?{
          ????return?new?Promise((resolve,?reject)?=>?{
          ????????axios
          ????????????.get(url)
          ????????????.then(res?=>?{
          ????????????????resolve(res.data)
          ????????????})
          ????????????.catch(err?=>?{
          ????????????????reject(err.data)
          ????????????})
          ????})
          }

          vue-cli下簡(jiǎn)單的封裝axios

          1. 安裝?axios
          npm?install?axios?--save
          1. main.js引入?axios
          import?axios?from?'axios'

          Vue.prototype.$axios?=?axios

          //默認(rèn)請(qǐng)求頭
          axios.defaults.headers.post['Content-Type']?=?'application/x-www-form-urlencoded'
          1. 使用axios
          //拼接參數(shù)
          this.$axios.get('/user?ID=12345')
          ??.then(function?(response)?{
          ????console.log(response);
          ??})
          ??.catch(function?(error)?{
          ????console.log(error);
          ??});

          //對(duì)象傳參
          this.$axios.get('/user',?{
          ????params:?{
          ??????ID:?12345
          ????}
          ??})
          ??.then(function?(response)?{
          ????console.log(response);
          ??})
          ??.catch(function?(error)?{
          ????console.log(error);
          ??});

          //或者使用箭頭函數(shù)
          this.$axios.get('/user?ID=12345')
          ??.then((response)?=>?{
          ????console.log(response);
          ??})
          ??.catch((error)?=>?{
          ????console.log(error);
          ??});

          //執(zhí)行多個(gè)并發(fā)請(qǐng)求
          function?getUserAccount()?{
          ??return?this.$axios.get('/user/12345');
          }
          function?getUserPermissions()?{
          ??return?this.$axios.get('/user/12345/permissions');
          }
          axios.all([getUserAccount(),?getUserPermissions()])
          ??.then(this.$axios.spread(function?(acct,?perms)?{
          ????//兩個(gè)請(qǐng)求現(xiàn)已完成
          ??}));
          1. 新建文件封裝axios
          //引入axios
          import?axios?from?'axios'

          //通過promise?封裝
          export?function?request(config){
          ????return?new?Promise((resolve,?reject)?=>?{

          ????????//創(chuàng)建axios實(shí)例
          ????????const?instance?=?axios.create({
          ????????????baseURL:'http://localhost:8080/api',
          ????????????timeout:5000
          ????????})

          ????????//發(fā)送請(qǐng)求
          ????????instance(config)
          ????????.then(res?=>?{
          ????????????resolve(res);
          ????????})
          ????????.catch(err?=>?{
          ????????????reject(err);
          ????????})
          ????})
          }
          1. 引用axios
          import?{?request?}?from?'../../api/request'

          request({
          ??????url:?'/showbanners/index',
          ??????method:?'get'
          ????})
          ??????.then((res)?=>?{
          ???????console.log(res)
          ??????})
          ??????.catch((err)?=>?{
          ????????console.log(err)
          ??????})
          1. 添加攔截器
          import?axios?from?'axios'

          //通過promise封裝
          export?function?request?(config)?{
          ??return?new?Promise((resolve,?reject)?=>?{

          ????//創(chuàng)建axios實(shí)例
          ????const?instance?=?axios.create({
          ??????baseURL:?'http://localhost:8080/api',
          ??????timeout:?5000
          ????})

          ????//?請(qǐng)求攔截
          ????instance.interceptors.request.use(config?=>?{
          ??????return?config
          ????},
          ????err?=>?{
          ??????console.log(err)
          ????})

          ????//?響應(yīng)攔截
          ????instance.interceptors.response.use(result?=>?{
          ??????console.log('攔截?cái)?shù)據(jù):=>',?result)
          ??????return?result
          ????},
          ????err?=>?{
          ??????console.log(err)
          ????})

          ????//發(fā)送請(qǐng)求
          ????instance(config)
          ??????.then(res?=>?{
          ????????resolve(res)
          ??????})
          ??????.catch(err?=>?{
          ????????reject(err)
          ??????})
          ??})
          }



          瀏覽 54
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产无码做爱视频 | 青青操青青操在线视频免费 | 日本熟妇一二三区视频 | 12一15女人A片毛 | 欧美日韩国产无码 |