1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        axios的簡單封裝

        共 3453字,需瀏覽 7分鐘

         ·

        2021-03-09 19:52

        html中簡單封裝axios

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

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

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

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

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


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

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

        vue-cli下簡單的封裝axios

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

        Vue.prototype.$axios?=?axios

        //默認請求頭
        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);
        ??});

        //對象傳參
        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í)行多個并發(fā)請求
        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)?{
        ????//兩個請求現(xiàn)已完成
        ??}));
        1. 新建文件封裝axios
        //引入axios
        import?axios?from?'axios'

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

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

        ????????//發(fā)送請求
        ????????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實例
        ????const?instance?=?axios.create({
        ??????baseURL:?'http://localhost:8080/api',
        ??????timeout:?5000
        ????})

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

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

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



        瀏覽 54
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            日韩h视频 | 久久99国产精品露脸老师 | 日韩午夜福利视频 | 欧美日韩国产二区 | 国产人妖TS重口系列 | 黄色录像欧美 | 女人又爽又黄免费女仆 | 无码人妻久久一区二区三区蜜桃 | 特黄特色大片免费播放叫疼 | 台湾毛片 |