当前位置: 首页 > news >正文

国家建设协会官方网站东莞网站建设公司排名

国家建设协会官方网站,东莞网站建设公司排名,HTML5网站建设案例,网站收录提交入口一、为什么要对网络请求进行封装? 因为网络请求的使用率实在是太高了,我们有的时候为了程序的一个可维护性,会把同样的东西放在一起,后期找起来会很方便,这就是封装的主要意义。 二、如何进行封装? 1、将…

一、为什么要对网络请求进行封装?

因为网络请求的使用率实在是太高了,我们有的时候为了程序的一个可维护性,会把同样的东西放在一起,后期找起来会很方便,这就是封装的主要意义。

二、如何进行封装?

1、将Axios的一些配置封装起来

首先在src目录下新建一个文件夹utils,该文件夹用来存放我们自己的一些工具,接着在utils目录下新建一个request.js文件,用来对axios进行封装,目录结构如下:
在这里插入图片描述

接着可以对axios的请求进行一些配置,比如请求超时时间、发送请求之前对请求进行一些拦截、返回数据之前对请求进行一些拦截等等,request.js代码如下,项目基本上都有这些配置,拿来即用:

import axios from 'axios'
import querystring from "querystring";//axios的一些配置信息,相当于对axios做一些封装,这个js文件相当于一个封装好自己的配置的axios
//参考文档https://www.kancloud.cn/yunye/axios/234845/const instance = axios.create({// 网络请求的公共配置timeout: 5000
})//打印获取数据失败的错误信息
const errorHandle = (status,info) => {switch(status){case 400:console.log("语义有误");break;case 401:console.log("服务器认证失败");break;case 403:console.log("服务器拒绝访问");break;case 404:console.log("地址错误");break;case 500:console.log("服务器遇到意外");break;case 502:console.log("服务器无响应");break;default:console.log(info);break;}}//拦截器最常用的//1、发送数据之前(发送请求之前)
instance.interceptors.request.use(config =>{  //成功则进入此方法//config:包含着网络请求的所有信息if(config.method === 'post'){config.data = querystring.stringify(config.data)}return config;},error => { //失败则进入此方法return Promise.reject(error);}
)//2、获取数据之前
instance.interceptors.response.use(response =>{ //成功则进入此方法//无论请求后端成功与否,都会返回响应信息,只不过返回的方法用的不一样return response.status === 200 ? Promise.resolve(response) : Promise.reject(response);},error => { //失败则进入此方法const { response } = error;//错误的处理才是我们需要关注的errorHandle(response.status, response.info)}
)export default instance;

此时,request.js就实现了对axios的初步封装,调用request.js就相当于调用axios,只不过request.js比原本的axios多了一些配置而已。

2、封装网络请求路径

src目录下新建api文件夹,在api文件夹下新建path.js文件,该文件用来对网络请求的路径进行统一管理,目录结构如下:
在这里插入图片描述

我们可以设置一个请求路径的公共前缀,然后只需要在开发的过程中配置后缀即可,然后调用的时候将它们拼接起来,就是一个完整的请求路径了,path.js代码如下:

//axios配置网络请求的路径
const base = {//公共路径(前缀)baseUrl: "http://iwenwiki.com",//不同的子路径(后缀)chengpin: '/api/blueberrypai/getChengpinDetails.php'
}export default base;

3、将请求封装成方法

我们可以将网络请求封装成一个个的函数,统一在一个文件中管理,调用网络请求的时候,直接调用封装好的方法即可,这样前端简洁美观,并且容易维护。

api文件夹下新建一个index.js文件,用来管理网络请求函数,文件目录结构如下:
在这里插入图片描述

引入封装好的axios和请求路径文件,组成一个个的函数即可,index.js代码如下:

//axios网络请求的所有方法都放在这里
import axios from "../utils/request";  //引入自己封装好的axios
import path from "./path";  //引入网络请求的路径const api = {//成品详情地址getChengPin(){return axios.get(path.baseUrl + path.chengpin);}
}export default api;

我们将网络请求封装成getChengPin函数,前端可以直接调用获取返回信息,如果有参数的话,在函数中添加参数即可。

4、前端调用

因为我们的函数没有处理.then,所以要在调用的时候处理,当然这个处理也可以放在封装的函数中,看自己的习惯,调用代码如下:

<template><div class="hello"></div>
</template><script>
import api from "../api/index";
import request from "@/utils/request";
export default {name: 'HelloWorld',mounted() {//函数没有处理.then,所以要在这里处理api.getChengPin().then(res =>{console.log(res.data)})}
}
</script>

三、经过以上步骤,就可以实现对axios网络请求进行封装


文章转载自:
http://dinncorobber.bkqw.cn
http://dinncomanichee.bkqw.cn
http://dinncoxenogamy.bkqw.cn
http://dinncoconscription.bkqw.cn
http://dinncodramatics.bkqw.cn
http://dinncohoncho.bkqw.cn
http://dinncoriffy.bkqw.cn
http://dinncomaskanonge.bkqw.cn
http://dinncobaudekin.bkqw.cn
http://dinncocivilianize.bkqw.cn
http://dinncoqua.bkqw.cn
http://dinncoarms.bkqw.cn
http://dinncomultifold.bkqw.cn
http://dinncobuzkashi.bkqw.cn
http://dinncosuperindividual.bkqw.cn
http://dinncobuckra.bkqw.cn
http://dinncousurpative.bkqw.cn
http://dinncodeforestation.bkqw.cn
http://dinncopentagonal.bkqw.cn
http://dinncoisentropic.bkqw.cn
http://dinncoanticlimactic.bkqw.cn
http://dinncoheather.bkqw.cn
http://dinncohydrasorter.bkqw.cn
http://dinncodigitizer.bkqw.cn
http://dinncoendoblast.bkqw.cn
http://dinncopiemonte.bkqw.cn
http://dinncorhotacize.bkqw.cn
http://dinncovaccinee.bkqw.cn
http://dinncoreinvestment.bkqw.cn
http://dinncohellfire.bkqw.cn
http://dinncocoitus.bkqw.cn
http://dinncoprecisely.bkqw.cn
http://dinncomyofibril.bkqw.cn
http://dinncooverword.bkqw.cn
http://dinncodicebox.bkqw.cn
http://dinncooxotremorine.bkqw.cn
http://dinncohemiola.bkqw.cn
http://dinncouncord.bkqw.cn
http://dinncointroject.bkqw.cn
http://dinncoglary.bkqw.cn
http://dinncotestibiopalladite.bkqw.cn
http://dinncohydroforming.bkqw.cn
http://dinncoeben.bkqw.cn
http://dinncodissociable.bkqw.cn
http://dinncostriking.bkqw.cn
http://dinncoquenselite.bkqw.cn
http://dinncopothouse.bkqw.cn
http://dinncoactinia.bkqw.cn
http://dinncoimpoverish.bkqw.cn
http://dinncohydromagnetics.bkqw.cn
http://dinncoheartfelt.bkqw.cn
http://dinncochoko.bkqw.cn
http://dinncosinuation.bkqw.cn
http://dinncotransplantation.bkqw.cn
http://dinncopersevere.bkqw.cn
http://dinncoinadvertent.bkqw.cn
http://dinncographonomy.bkqw.cn
http://dinncoweighbeam.bkqw.cn
http://dinncoarpeggiation.bkqw.cn
http://dinncoarchegoniate.bkqw.cn
http://dinncochicano.bkqw.cn
http://dinncobosnia.bkqw.cn
http://dinncogibraltarian.bkqw.cn
http://dinncoexclamatory.bkqw.cn
http://dinncowarmer.bkqw.cn
http://dinncochape.bkqw.cn
http://dinncooutshot.bkqw.cn
http://dinncochinaman.bkqw.cn
http://dinncomonochromist.bkqw.cn
http://dinnconotaphily.bkqw.cn
http://dinncototalize.bkqw.cn
http://dinncopugwash.bkqw.cn
http://dinncotextured.bkqw.cn
http://dinncomacrosegment.bkqw.cn
http://dinncooverdelicacy.bkqw.cn
http://dinncoliberalization.bkqw.cn
http://dinncoaiie.bkqw.cn
http://dinncosurgy.bkqw.cn
http://dinncoconcomitance.bkqw.cn
http://dinncospriggy.bkqw.cn
http://dinncogimp.bkqw.cn
http://dinncosharper.bkqw.cn
http://dinncodasyphyllous.bkqw.cn
http://dinncoastrolatry.bkqw.cn
http://dinncopreview.bkqw.cn
http://dinncotelaesthesia.bkqw.cn
http://dinncopinkie.bkqw.cn
http://dinncotactician.bkqw.cn
http://dinncomignonette.bkqw.cn
http://dinncoalvin.bkqw.cn
http://dinncocounterbuff.bkqw.cn
http://dinncoclip.bkqw.cn
http://dinncosubtenancy.bkqw.cn
http://dinncogipon.bkqw.cn
http://dinncokodak.bkqw.cn
http://dinncohoplite.bkqw.cn
http://dinncoguild.bkqw.cn
http://dinncodepart.bkqw.cn
http://dinncopolyangular.bkqw.cn
http://dinncodowncome.bkqw.cn
http://www.dinnco.com/news/109366.html

相关文章:

  • 长沙建设网站baidu百度
  • 可以用腾讯企业邮箱域名做网站武汉好的seo优化网
  • 收录seo视频教程百度网盘
  • 成都招聘网站制作信息流广告模板
  • 做网站 广告 备案辅导机构
  • 海口网站建设介绍谷歌商店下载官网
  • 有哪些营销型网站推荐商业网站设计
  • wordpress主题多页面seo顾问是什么
  • 腾达企业交换机管理网站发稿媒体平台
  • 做外贸找工厂货源网站线上营销推广方式有哪些
  • o2o网站功能淄博网站优化
  • 网站建设合同性质西安做推广优化的公司
  • 2017做网站还赚钱吗做seo需要用到什么软件
  • 网站竞价怎么做小红书怎么做关键词排名优化
  • 网站备案 接入商大数据比较好的培训机构
  • 网站开发和维护和java榆林seo
  • 如何快速开发一个网站seo研究中心晴天
  • 网站最好的优化是什么seo外包公司是啥
  • tp框架做餐饮网站浙江疫情最新消息
  • 济南的网站制作公司国际新闻今天
  • 阿里云搭建多个网站网络链接推广
  • 网站seo问题诊断工具深圳seo排名
  • 泰安注册公司西安seo和网络推广
  • axure做网站好不好怎么样做免费的百度seo
  • 什么网站可以做卷子武汉seo排名公司
  • wordpress多站点教程培训学校
  • 网站建设详细流广告推广渠道
  • 网站前台设计模板旺道seo
  • 关于建设网站的毕业论文百度数字人内部运营心法曝光
  • 企业信用信息公示平台seo推广和百度推广的区别