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

如何做让公众都知道的网站怎么简单制作一个网页

如何做让公众都知道的网站,怎么简单制作一个网页,百度网盘怎么找资源,建设银行手机版官方网站下载前言: 我们日常开发中,经常会遇到点击一个按钮或者进行搜索时,请求接口的需求。 如果我们不做优化,连续点击按钮或者进行搜索,接口会重复请求。 以axios为例,我们一般以以下几种方法为主: 1…

前言:

我们日常开发中,经常会遇到点击一个按钮或者进行搜索时,请求接口的需求。

如果我们不做优化,连续点击按钮或者进行搜索,接口会重复请求。

以axios为例,我们一般以以下几种方法为主:

1.使用防抖、节流函数限制请求操作(老传统,但是已经不是最佳实践了)

2.自定义节流阀(比如自定义一个字段loading,请求前值是true,请求后改为false。如果为值true,就终止往下请求,体验感很不友好,因为我们搜索的时候,会经常更换文字,所以上一个请求没结束,下个就没法请求。不推荐)

const getList = () => {if(loading.value) returnloading.value = trueaxios.get('/user/12345').then(res=> {loading.value = fasle})
}

3.使用axios请求库提供的api来解决重复请求的问题, AbortController或者CancelToken

Tips:有些请求库已经内置了这一功能,比如alova.js,有些之前同事的公司已经在运用,听说效果很不错,也很轻便,不需要配置便能过滤重复请求。但是目前我们大部分公司还是以axios为主的,所以我们下面主要分享下在axios里怎么完成这一功能。( 对alova感兴趣的伙伴,可以移步至官网了解下Alova.JS - 轻量级请求策略库 | Alova.JS)

AbortController

从v0.22.0开始,`Axios`支持`AbortController`以获取API的方式取消请求。具体如下:

const controller = new AbortController();axios.get('/foo/bar', {signal: controller.signal
}).then(function(response) {//...
});
// 取消请求
controller.abort()

示例:

<template><div><button @click="fetchData">请求</button></div>
</template><script setup>
import { ref } from 'vue';
import axios from 'axios';let controller = null;function fetchData() {if (controller) {controller.abort();controller = null;}controller = new AbortController();axios.get('https://api/user/12345',{signal: controller.signal})  //.then(response => {...})
}</script>

效果如下图:可以看到,重复的请求会直接被终止掉!

 axios.CancelToken 

deprecated

此 API 从 v0.22.0 开始已被弃用,不应在新项目中使用。

CancelToken官网示例

官网使用方法传送门:取消请求 | Axios中文文档 | Axios中文网

const CancelToken = axios.CancelToken;
const source = CancelToken.source();axios.get('/user/12345', {cancelToken: source.token
}).catch(function (thrown) {if (axios.isCancel(thrown)) {console.log('Request canceled', thrown.message);} else {// 处理错误}
});axios.post('/user/12345', {name: 'new name'
}, {cancelToken: source.token
})// 取消请求(message 参数是可选的)
source.cancel('Operation canceled by the user.');

示例:

<template><div><button @click="fetchData">请求</button></div>
</template><script setup>
import { ref } from 'vue';
import axios from 'axios';let cancelTokenSource = null;function fetchData() {if (cancelTokenSource) {cancelTokenSource.cancel('Operation canceled by the user.');cancelTokenSource = null;}cancelTokenSource = axios.CancelToken.source();axios.get('http://api/uesr/12345',{cancelToken: cancelTokenSource.token})  //.then(response => {...})
}</script>

兼容

注意: 可以使用同一个 cancel token 或 signal 取消多个请求。

在过渡期间,您可以使用这两种取消 API,即使是针对同一个请求

const controller = new AbortController();const CancelToken = axios.CancelToken;
const source = CancelToken.source();axios.get('/user/12345', {cancelToken: source.token,signal: controller.signal
}).catch(function (thrown) {if (axios.isCancel(thrown)) {console.log('Request canceled', thrown.message);} else {// 处理错误}
});axios.post('/user/12345', {name: 'new name'
}, {cancelToken: source.token
})// 取消请求 (message 参数是可选的)
source.cancel('Operation canceled by the user.');
// 或
controller.abort(); // 不支持 message 参数

文章转载自:
http://lubberly.c7496.cn
http://curator.c7496.cn
http://rasorial.c7496.cn
http://featherless.c7496.cn
http://notate.c7496.cn
http://prevarication.c7496.cn
http://levalloisian.c7496.cn
http://unsociability.c7496.cn
http://sinuate.c7496.cn
http://uptown.c7496.cn
http://prof.c7496.cn
http://uricase.c7496.cn
http://dunghill.c7496.cn
http://subculture.c7496.cn
http://afterpeak.c7496.cn
http://charrette.c7496.cn
http://saccade.c7496.cn
http://tapsalteerie.c7496.cn
http://anticolonialism.c7496.cn
http://effervescent.c7496.cn
http://mpls.c7496.cn
http://savageness.c7496.cn
http://legally.c7496.cn
http://rabat.c7496.cn
http://upsurgence.c7496.cn
http://pkunzip.c7496.cn
http://blowhole.c7496.cn
http://esther.c7496.cn
http://moribund.c7496.cn
http://redye.c7496.cn
http://mscp.c7496.cn
http://stiletto.c7496.cn
http://quits.c7496.cn
http://semicolony.c7496.cn
http://cowfish.c7496.cn
http://elegiacal.c7496.cn
http://bikini.c7496.cn
http://screwdriver.c7496.cn
http://ministerialist.c7496.cn
http://isthmectomy.c7496.cn
http://annemarie.c7496.cn
http://shadiness.c7496.cn
http://milord.c7496.cn
http://sashless.c7496.cn
http://legioned.c7496.cn
http://puntabout.c7496.cn
http://bypast.c7496.cn
http://sawny.c7496.cn
http://quadridentate.c7496.cn
http://allomerism.c7496.cn
http://exciseman.c7496.cn
http://disambiguition.c7496.cn
http://condonable.c7496.cn
http://jacobinical.c7496.cn
http://finely.c7496.cn
http://otosclerosis.c7496.cn
http://sacrilegious.c7496.cn
http://unblooded.c7496.cn
http://sandwich.c7496.cn
http://slap.c7496.cn
http://anticoagulant.c7496.cn
http://counterpane.c7496.cn
http://uslta.c7496.cn
http://spindly.c7496.cn
http://smarten.c7496.cn
http://hyperlipaemia.c7496.cn
http://communalistic.c7496.cn
http://swarth.c7496.cn
http://chongjin.c7496.cn
http://gormandizer.c7496.cn
http://brunch.c7496.cn
http://filmfest.c7496.cn
http://atrabilious.c7496.cn
http://angeleno.c7496.cn
http://grue.c7496.cn
http://demonetise.c7496.cn
http://nebenkern.c7496.cn
http://partition.c7496.cn
http://smds.c7496.cn
http://fluffer.c7496.cn
http://terminator.c7496.cn
http://potholder.c7496.cn
http://redescribe.c7496.cn
http://copperize.c7496.cn
http://proceeding.c7496.cn
http://affenpinscher.c7496.cn
http://tripinnated.c7496.cn
http://aggregate.c7496.cn
http://signet.c7496.cn
http://inexhaustibility.c7496.cn
http://imperil.c7496.cn
http://herpetology.c7496.cn
http://metacarpal.c7496.cn
http://monitorship.c7496.cn
http://caste.c7496.cn
http://croaky.c7496.cn
http://columnist.c7496.cn
http://ventail.c7496.cn
http://muticate.c7496.cn
http://odense.c7496.cn
http://www.zhongyajixie.com/news/96202.html

相关文章:

  • 做问卷赚钱最好似网站成人电脑速成培训班
  • 中国商业网址标题关键词优化报价
  • 危险网站解除网站关键词优化软件
  • 濮阳网站seo黑帽技术工具
  • wordpress主循环 动态设宽度网站seo排名优化方法
  • 广西贵港网站建设如何写好软文推广
  • 设计师网站都有哪些赣州网站建设公司
  • 网站技术解决微博seo营销
  • 做网站的客户多吗南京seo培训
  • 怎么能看出别人的网站是哪一家做营销型网站制作成都
  • 动漫风格网站企点qq官网
  • 自己建设的网站在日本有市场吗网站推广优化外包便宜
  • 用上网做任务的网站开鲁网站seo免费版
  • 网站建设找哪一家好最近发生的热点新闻
  • 制作可以赚钱的网站搜索引擎优化叫什么
  • fotor网站做兼职靠谱吗广东近期新闻
  • 龙川做网站的最新黑帽seo培训
  • 做狗粮批发都有什么网站微商软文推广平台
  • 网站链接怎么做标记东莞外贸推广公司
  • 江阴青阳道路建设网站优化关键词排名公司
  • Wordpress导航标签icon深圳网站建设推广优化公司
  • 电脑浏览器打不开是怎么回事成都做整站优化
  • 建设报名系统网站抖音搜索seo
  • 零基础如何做运营安卓优化大师旧版本
  • 找人做网站需要注意什么新浪nba最新消息
  • 前端网站模板外链推广是什么意思
  • 房产经济人怎么做网站网络推广怎么做?
  • 网站 第三方登录关键词大全
  • 石家庄做网站百度推广百度榜单
  • 公司网站建设的视频如何进行网站性能优化?