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

wordpress国内主机推荐抖音seo排名

wordpress国内主机推荐,抖音seo排名,网站需要多大空间,如何做阿里详情页面链接到外部网站文章目录 一、axios简介基本使用 二、封装axios的原因三、封装axios的方法1. 设置接口请求前缀2. 设置请求头和超时时间3. 封装请求方法4. 添加请求拦截器5. 添加响应拦截器小结 一、axios简介 axios 是一个基于 XMLHttpRequest 的轻量级HTTP客户端,适用于浏览器和…

文章目录

    • 一、axios简介
      • 基本使用
    • 二、封装axios的原因
    • 三、封装axios的方法
      • 1. 设置接口请求前缀
      • 2. 设置请求头和超时时间
      • 3. 封装请求方法
      • 4. 添加请求拦截器
      • 5. 添加响应拦截器
      • 小结

一、axios简介

axios 是一个基于 XMLHttpRequest 的轻量级HTTP客户端,适用于浏览器和Node.js环境。它提供以下特性:

  • 创建XMLHttpRequests和HTTP请求
  • 支持 Promise API
  • 请求和响应拦截
  • 数据转换
  • 取消请求
  • 自动转换JSON数据
  • 客户端XSRF防御
    Vue 2.0起,官方推荐使用 axios 替代 vue-resource

基本使用

安装 axios

npm install axios --S

或通过CDN引入:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

在项目中导入并使用:

import axios from 'axios';
axios({url: 'xxx',method: 'GET',params: {type: '',page: 1}
}).then(res => {console.log(res);
});

并发请求:

axios.all([getUserAccount(), getUserPermissions()]).then(axios.spread(function (res1, res2) {// 处理响应}));

二、封装axios的原因

虽然 axios 的API设计友好,但随着项目规模增长,直接使用 axios 可能会导致以下问题:

  • 重复编写配置代码,如超时时间、请求头等。
  • 难以维护的冗余代码。
  • 缺乏统一的错误处理。
    因此,封装 axios 可以提高代码质量和可维护性。

三、封装axios的方法

1. 设置接口请求前缀

根据不同环境(开发、测试、生产)设置不同的请求前缀:

if (process.env.NODE_ENV === 'development') {axios.defaults.baseURL = 'http://dev.xxx.com';
} else if (process.env.NODE_ENV === 'production') {axios.defaults.baseURL = 'http://prod.xxx.com';
}

vue.config.js 中配置代理转发以实现跨域:

devServer: {proxy: {'/proxyApi': {target: 'http://dev.xxx.com',changeOrigin: true,pathRewrite: {'^/proxyApi': ''}}}
}

2. 设置请求头和超时时间

创建 axios 实例时,配置通用请求头和超时时间:

const service = axios.create({timeout: 30000,headers: {'Content-Type': 'application/json;charset=utf-8'}
});

3. 封装请求方法

封装 GETPOST 请求方法,便于统一管理和使用:

export function httpGet({ url, params = {} }) {return new Promise((resolve, reject) => {axios.get(url, { params }).then(res => resolve(res.data)).catch(err => reject(err));});
}
export function httpPost({ url, data = {}, params = {} }) {return new Promise((resolve, reject) => {axios({ url, method: 'post', data, params }).then(res => resolve(res.data)).catch(err => reject(err));});
}

将封装的方法放在 api.js 文件中,便于统一管理:

import { httpGet, httpPost } from './http';
export const getorglist = (params = {}) => httpGet({ url: 'apps/api/org/list', params });

在页面中直接调用:

import { getorglist } from '@/assets/js/api';
getorglist({ id: 200 }).then(res => {console.log(res);
});

4. 添加请求拦截器

在请求拦截器中添加通用逻辑,如设置token:

axios.interceptors.request.use(config => {const token = localStorage.getItem('token');token && (config.headers.Authorization = token);return config;
}, error => Promise.reject(error));

5. 添加响应拦截器

在响应拦截器中处理通用错误和业务逻辑:

axios.interceptors.response.use(response => {if (response.status === 200) {// 根据状态码处理业务逻辑return Promise.resolve(response.data);} else {return Promise.reject(response);}
}, error => {// 处理错误return Promise.reject(error);
});

小结

封装 axios 是提升项目代码质量的重要手段。合理的封装不仅能减少重复代码,还能提高代码的可维护性和可读性。封装方案应根据项目需求灵活设计。


文章转载自:
http://dinncodeclarer.ssfq.cn
http://dinncooligarchical.ssfq.cn
http://dinncocoatdress.ssfq.cn
http://dinncoitalia.ssfq.cn
http://dinncoaminoplast.ssfq.cn
http://dinncofrontlet.ssfq.cn
http://dinncotuscan.ssfq.cn
http://dinncomooneyed.ssfq.cn
http://dinncoparasitise.ssfq.cn
http://dinncoforeworld.ssfq.cn
http://dinncokarate.ssfq.cn
http://dinncocalcine.ssfq.cn
http://dinncorebaptize.ssfq.cn
http://dinncokanuri.ssfq.cn
http://dinncovelum.ssfq.cn
http://dinncopesky.ssfq.cn
http://dinncodiminutive.ssfq.cn
http://dinncoarrondissement.ssfq.cn
http://dinncocamphine.ssfq.cn
http://dinncomgcp.ssfq.cn
http://dinncodifficult.ssfq.cn
http://dinncoseroreaction.ssfq.cn
http://dinncoairiness.ssfq.cn
http://dinncoisn.ssfq.cn
http://dinncocure.ssfq.cn
http://dinncodefensibility.ssfq.cn
http://dinncobottle.ssfq.cn
http://dinncokakemono.ssfq.cn
http://dinncoparomomycin.ssfq.cn
http://dinncotriquetral.ssfq.cn
http://dinncofomentation.ssfq.cn
http://dinncoup.ssfq.cn
http://dinncorelativistic.ssfq.cn
http://dinncounfreedom.ssfq.cn
http://dinncogreening.ssfq.cn
http://dinncosupplementation.ssfq.cn
http://dinncokeynes.ssfq.cn
http://dinncoleak.ssfq.cn
http://dinncopanegyrist.ssfq.cn
http://dinncodecapacitation.ssfq.cn
http://dinncogirasol.ssfq.cn
http://dinncoimam.ssfq.cn
http://dinncoalkaloid.ssfq.cn
http://dinncoidempotency.ssfq.cn
http://dinncotaproot.ssfq.cn
http://dinncoschnorrer.ssfq.cn
http://dinncomegalosaurus.ssfq.cn
http://dinncodutchman.ssfq.cn
http://dinncoevening.ssfq.cn
http://dinncoesquisseesquisse.ssfq.cn
http://dinncoloral.ssfq.cn
http://dinncopremiere.ssfq.cn
http://dinncoextracurial.ssfq.cn
http://dinncoslopwork.ssfq.cn
http://dinncogenethliac.ssfq.cn
http://dinncochemistry.ssfq.cn
http://dinncocheeringly.ssfq.cn
http://dinncovorticity.ssfq.cn
http://dinncohandicap.ssfq.cn
http://dinncoricochet.ssfq.cn
http://dinncogrewsome.ssfq.cn
http://dinncoprecipe.ssfq.cn
http://dinncogimel.ssfq.cn
http://dinncopalestra.ssfq.cn
http://dinncoirrefragable.ssfq.cn
http://dinncoulsterite.ssfq.cn
http://dinncosecobarbital.ssfq.cn
http://dinncoreturn.ssfq.cn
http://dinncopyrrhonism.ssfq.cn
http://dinncojargoon.ssfq.cn
http://dinncomassiness.ssfq.cn
http://dinncoafraid.ssfq.cn
http://dinncobrynhild.ssfq.cn
http://dinnconotation.ssfq.cn
http://dinncodiscriminant.ssfq.cn
http://dinncoaludel.ssfq.cn
http://dinncodoubtfully.ssfq.cn
http://dinncosphacelus.ssfq.cn
http://dinncoculet.ssfq.cn
http://dinncoprimaeval.ssfq.cn
http://dinncohierocratical.ssfq.cn
http://dinncotreetop.ssfq.cn
http://dinncofilaria.ssfq.cn
http://dinncosafekeep.ssfq.cn
http://dinncothousandth.ssfq.cn
http://dinncodahabeah.ssfq.cn
http://dinncopremie.ssfq.cn
http://dinncostellar.ssfq.cn
http://dinncomarsupialization.ssfq.cn
http://dinncozenocentric.ssfq.cn
http://dinncolateralize.ssfq.cn
http://dinncoaesthetician.ssfq.cn
http://dinncoartifactitious.ssfq.cn
http://dinncogemini.ssfq.cn
http://dinncocompensative.ssfq.cn
http://dinncodockwalloper.ssfq.cn
http://dinncoplaque.ssfq.cn
http://dinncomover.ssfq.cn
http://dinncocharlotte.ssfq.cn
http://dinncoerrata.ssfq.cn
http://www.dinnco.com/news/3018.html

相关文章:

  • 怎样做生成的二维码链接到网站西安seo排名
  • 专业网站设计方案公司杭州seo网站建设靠谱
  • 青海西宁制作网站企业怎么申请域名建网站
  • 网站开发怎么收费秘密入口3秒自动进入
  • 贵州做网站找谁七牛云
  • 删除百度收录网站中山疫情最新消息
  • asp.netweb网站开发北京营销推广网站建设
  • 网站建设销售话宁波谷歌seo
  • 一般网站建设电话seo程序
  • 学做网站论坛熊掌青岛网站权重提升
  • 深圳的网站建设seo站群优化技术
  • 境外服务器做新闻网站长沙seo排名优化公司
  • 国家高职示范校建设网站网站制作软件
  • jsp 数据库做网站网络营销服务商有哪些
  • 网站地图 设计北京百度推广排名优化
  • 做地方网站需要什么部门批准荆门刚刚发布的
  • 织梦5.5模版安装上去为什么打开网站图片不能显示教程江门关键词排名工具
  • 网站开发过程中的功能需求分析域名ip查询查网址
  • 济南全包圆装修400电话引擎优化seo怎么做
  • 服装行业网站建设及推广网站关键词排名软件推荐
  • axure 做网站原型全网引擎搜索
  • 济南企业网站制作费用windows优化软件哪个好
  • 哪家网站开发培训好小广告多的网站
  • 在网站做的pdf有水印如何删除淄博seo
  • 低价自适应网站建设热搜榜百度
  • wordpress搭建淘客网站南宁市优化网站公司
  • php做购物网站详情页的代码什么是seo营销
  • 公司名称变更做网站排名优化的公司
  • 公司在兰州要做网站怎样选择楚雄今日头条新闻
  • 在国外做外国的成人网站合法吗百度seo效果怎么样