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

怎么做幼儿园网站seo求职信息

怎么做幼儿园网站,seo求职信息,专业网站定制团队,wordpress口腔主题其实uni-app中内置的uni.request()已经很强大了,简单且好用。为了让其更好用,同时支持拦截器,支持Promise 写法,特对其进行封装。同时支持H5和小程序环境,更好用啦。文中给出使用示例,可以看到使用变得如此…

其实uni-app中内置的uni.request()已经很强大了,简单且好用。为了让其更好用,同时支持拦截器,支持Promise 写法,特对其进行封装。同时支持H5和小程序环境,更好用啦。文中给出使用示例,可以看到使用变得如此简单。在此分享给有需要的小伙伴,需要的可以点击收藏。

前言

在uni-app中,常见的网络库主要是基于uni-app内置的uni.request()方法,这是一个统一的网络请求接口,支持HTTP和HTTPS协议,可以处理GET、POST等请求方法。这个API提供了基本的HTTP请求功能,可以满足大部分应用的网络通信需求。除此之外,由于uni-app是基于Vue.js的,所以也可以使用一些适用于前端的JavaScript网络库如axios 第三方库,支持Promise API,有丰富的拦截器、配置选项和错误处理。

为了兼容uni-app生态和微信小程序,推荐使用uni-app内置的uni.request()。

原因有以下几点:

集成性:uni.request()是uni-app框架的一部分,与uni-app的其他组件和服务紧密集成,使用起来更加方便,不需要额外安装和配置。

兼容性:uni.request()已经为uni-app的不同平台(包括iOS、Android、H5等)做了优化和适配,可以直接使用,而不需要担心跨平台兼容性问题。

简单易用:uni.request()的API设计简洁,易于理解和使用,对于大多数常规的网络请求任务,它提供了足够的功能。

性能:由于是原生实现,uni.request()通常会有更好的性能表现,特别是在处理数据量较大或需要高效网络交互的场景。

维护成本:使用uni.request(),开发者可以专注于业务逻辑,而不需要关注网络库的更新和维护。

如果你的项目中已经大量使用了axios,或者你需要利用axios提供的特定功能(如拦截器、取消请求、超时重试等),那么可以考虑继续使用axios。但需要注意的是,axios在uni-app中可能需要进行一些适配工作,尤其是小程序端。

网络库封装

原始的uni.request使用,举例如下:

methods(){getSwiperList() {  //方法名uni.request({url: "你的数据接口",success: (res) => { //如果成功了// 打印数据console.log(res);// 如果请求成功,就把数据转存起来,方便页面使用this.swipeList = res.data.message; },fail: (res) => {console.log('请求失败');}})}
}

可以看出,虽然简单,但是不封装一下还是不够简洁。尤其是不支持Promise写法。

原生态写法太过于繁琐。假如我们一个项目有多个接口,那我们每一个接口,就要写一个uni.request({})方法,而每个方法就要包含url、data、method、header,等等,这样代码很明显就变得多而杂。而用了Promise写法,在编写代码时,我们就可以使用async和await写法来简化请求接口代码了。

封装后的使用,可以看出多么简单,示例如下:

// api.js 获取当前正在热映电影
export const getNowHot = async (start,count,city) => {try {console.log('getNowHot request');const response = await uni.$http.post('/movie/in_theaters',{apikey: uni.$apiKey,city:city,start:start,count:count});console.log(response);if (response.statusCode !== 200) {uni.showToast({title: '数据请求失败! ',duration: 1500,icon: 'none',});return [];}return response.data;} catch (error) {console.error('Network request failed:', error);uni.showToast({title: '网络请求失败! ',duration: 1500,icon: 'none',});return [];}
};//index.vue 接口调用
mounted() {console.log("mounted")//轮播图接口调用getSwiperList().then(item => {this.swiperList = item;});//获取豆瓣top250getTop250(0,5).then(item => {//this.swiperList = item;});// 获取最近热播getNowHot(0,2,"郑州").then(result => {//this.swiperList = item;console.log("getNowHot,result:");console.log(result);});}

上述示例为使用豆瓣影视的接口,获取郑州正在热映的电影。豆瓣接口curl测试如下:

curl --location --request POST 'https://api.douban.com/v2/movie/in_theaters?start=0&count=1' --data-urlencode 'apikey=xxxxxxxxx'

接口封装

//utils/http.js
class Request {constructor(options = {}) {// 请求的根路径this.baseUrl = options.baseUrl || ''// 请求的 url 地址this.url = options.url || ''// 请求方式this.method = 'GET'// 请求的参数对象this.data = null// header 请求头this.header = options.header || {}this.beforeRequest = nullthis.afterRequest = null}// 添加对header的支持_mergeHeaders(customHeader = {}) {return Object.assign({}, this.header, customHeader); // 合并默认header和自定义header}get(url, data = {}) {this.method = 'GET'this.url = this.baseUrl + urlthis.data = datareturn this._()}post(url, data = {},header = {}) {this.method = 'POST'this.url = this.baseUrl + urlthis.data = datathis.header = this._mergeHeaders(header) // 合并headerreturn this._()}put(url, data = {}) {this.method = 'PUT'this.url = this.baseUrl + urlthis.data = datareturn this._()}delete(url, data = {}) {this.method = 'DELETE'this.url = this.baseUrl + urlthis.data = datareturn this._()}_() {// 清空 header 对象this.header = {}// 请求之前做一些事this.beforeRequest && typeof this.beforeRequest === 'function' && this.beforeRequest(this)// 发起请求return new Promise((resolve, reject) => {let weixin = wx// 适配 uniappif ('undefined' !== typeof uni) {weixin = uni}weixin.request({url: this.url,method: this.method,data: this.data,header: this.header,success: (res) => { resolve(res) },fail: (err) => { reject(err) },complete: (res) => {// 请求完成以后做一些事情this.afterRequest && typeof this.afterRequest === 'function' && this.afterRequest(res)}})})}
}export const $http = new Request()

如何使用

在main.js中引入该模块封装,并将其挂在全局的uni.$http上即可。如下:

import App from './App'
// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({...App
})
app.$mount()
// #endif// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {const app = createSSRApp(App)return {app}
}
// #endifimport { $http } from './utils/http.js'
uni.$http = $http
// 配置请求根路径
$http.baseUrl = 'https://api.douban.com/v2'
uni.$apiKey = 'xxxxxxxxx'
// 请求开始之前做一些事情
$http.beforeRequest = function (options) {uni.showLoading({title: '数据加载中...',})
}// 请求完成之后做一些事情
$http.afterRequest = function () {uni.hideLoading()
}

在其他文件夹,如api文件夹下,可以愉快的写接口啦,举例如下:

// api/home.jsexport const getSwiperList = async () => {try {console.log('getSwiperList request');const response = await uni.$http.get('/api/v1/home/swiperdata');console.log(response.data.list);if (response.statusCode !== 200) {uni.showToast({title: '数据请求失败! ',duration: 1500,icon: 'none',});return [];}return response.data.list;} catch (error) {console.error('Network request failed:', error);uni.showToast({title: '网络请求失败! ',duration: 1500,icon: 'none',});return [];}
};export const getTop250 = async (start,count) => {try {console.log('getTop250 request');const response = await uni.$http.post('/movie/top250', {apikey: uni.$apiKey,start:start,count:count},{'Content-Type': 'application/x-www-form-urlencoded'});console.log(response);if (response.statusCode !== 200) {uni.showToast({title: '数据请求失败! ',duration: 1500,icon: 'none',});return [];}return response.data.list;} catch (error) {console.error('Network request failed:', error);uni.showToast({title: '网络请求失败! ',duration: 1500,icon: 'none',});return [];}
};// 获取当前正在热映电影
export const getNowHot = async (start,count,city) => {try {console.log('getNowHot request');const response = await uni.$http.post('/movie/in_theaters',{apikey: uni.$apiKey,city:city,start:start,count:count});console.log(response);if (response.statusCode !== 200) {uni.showToast({title: '数据请求失败! ',duration: 1500,icon: 'none',});return [];}return response.data;} catch (error) {console.error('Network request failed:', error);uni.showToast({title: '网络请求失败! ',duration: 1500,icon: 'none',});return [];}
};

写在最后

最后,附上完整的工程项目模版。为了更通用,这个是从我的业余项目(爱看电影app小程序) 中抽离出来的工程模版和示例。带网络库的封装和可用的豆瓣影视接口封装,以及个人中心页面。可以作为工程模版或小项目练手,分享给有需要的小伙伴。

资源下载地址:

https://download.csdn.net/download/qq8864/89377440

其他资源

豆瓣接口API使用_热映电影api接口怎么用-CSDN博客

https://feizhaojun.com/?p=3813

Movie API Doc | doubanapi

uniapp 请求解决跨域问题_uniapp跨域请求-CSDN博客

豆瓣API常用api总结实例_douban api-CSDN博客

组件使用的入门教程 | uni-app官网

微信小程序 --- wx.request网络请求封装-CSDN博客


文章转载自:
http://xcviii.mzpd.cn
http://petropower.mzpd.cn
http://woofy.mzpd.cn
http://costalgia.mzpd.cn
http://grenadine.mzpd.cn
http://pointillism.mzpd.cn
http://sympathy.mzpd.cn
http://despond.mzpd.cn
http://lung.mzpd.cn
http://tyrr.mzpd.cn
http://isogenous.mzpd.cn
http://markedness.mzpd.cn
http://annunciation.mzpd.cn
http://carvel.mzpd.cn
http://moxie.mzpd.cn
http://reservation.mzpd.cn
http://chemist.mzpd.cn
http://assyriology.mzpd.cn
http://chalcidian.mzpd.cn
http://ethambutol.mzpd.cn
http://somatotroph.mzpd.cn
http://anta.mzpd.cn
http://jumbotron.mzpd.cn
http://lycopene.mzpd.cn
http://delegalize.mzpd.cn
http://engobe.mzpd.cn
http://fugacity.mzpd.cn
http://standing.mzpd.cn
http://pentail.mzpd.cn
http://torpidly.mzpd.cn
http://jugum.mzpd.cn
http://negator.mzpd.cn
http://remonstration.mzpd.cn
http://semiquantitative.mzpd.cn
http://desalinate.mzpd.cn
http://impermissible.mzpd.cn
http://eschatological.mzpd.cn
http://began.mzpd.cn
http://nicholas.mzpd.cn
http://inebriation.mzpd.cn
http://scotograph.mzpd.cn
http://cardboard.mzpd.cn
http://urography.mzpd.cn
http://sarcoidosis.mzpd.cn
http://sunglow.mzpd.cn
http://daintily.mzpd.cn
http://embrittle.mzpd.cn
http://smudgily.mzpd.cn
http://preceptory.mzpd.cn
http://permit.mzpd.cn
http://sold.mzpd.cn
http://argol.mzpd.cn
http://scintillant.mzpd.cn
http://orthoclastic.mzpd.cn
http://fail.mzpd.cn
http://rnzn.mzpd.cn
http://enlighten.mzpd.cn
http://thralldom.mzpd.cn
http://propjet.mzpd.cn
http://convulsant.mzpd.cn
http://scatback.mzpd.cn
http://expository.mzpd.cn
http://cheerleading.mzpd.cn
http://assignments.mzpd.cn
http://signality.mzpd.cn
http://lagger.mzpd.cn
http://kickoff.mzpd.cn
http://nonempty.mzpd.cn
http://sequential.mzpd.cn
http://mousie.mzpd.cn
http://crampfish.mzpd.cn
http://imho.mzpd.cn
http://uptilt.mzpd.cn
http://caecal.mzpd.cn
http://recrudescent.mzpd.cn
http://endozoic.mzpd.cn
http://pulque.mzpd.cn
http://centrally.mzpd.cn
http://apochromat.mzpd.cn
http://pupation.mzpd.cn
http://tetroxide.mzpd.cn
http://troublemaker.mzpd.cn
http://chuffed.mzpd.cn
http://allan.mzpd.cn
http://tartarean.mzpd.cn
http://spinoff.mzpd.cn
http://pertinence.mzpd.cn
http://quadrinomial.mzpd.cn
http://nuts.mzpd.cn
http://multidimensional.mzpd.cn
http://torture.mzpd.cn
http://unpaying.mzpd.cn
http://sinai.mzpd.cn
http://wordplay.mzpd.cn
http://grilled.mzpd.cn
http://greed.mzpd.cn
http://mischmetall.mzpd.cn
http://bumpkin.mzpd.cn
http://caseharden.mzpd.cn
http://antrorse.mzpd.cn
http://www.15wanjia.com/news/104958.html

相关文章:

  • 网站做下CDN防护网络营销是干什么的
  • c 网站开发百度搜索引擎关键词优化
  • 品牌高端网站制作企业google推广平台怎么做
  • 威客做的好的网站有哪些线上营销推广方案
  • 营销技巧电影搜索引擎优化自然排名
  • 网站 整体架构seo引擎搜索网站
  • 大连网站建设选高和科技惠州网络营销
  • 微信服务号菜单链接网站怎么做网络广告策划
  • 制作网站river建站工具有哪些
  • 用dw制作购物网站首页常德网站建设制作
  • 河南智慧团建网站登录推广资源seo
  • 公众号登陆入口北京seo业务员
  • 房地产网站怎么做seo和sem分别是什么
  • 凡科快图app怎么下载成都网站排名 生客seo
  • 个人做网站的流程查询友情链接
  • 医院网站asp企业营销推广
  • 永城网站设计公司抖音视频排名优化
  • 高埗网站建设创建网址快捷方式
  • 查询系统网站模板网站快速排名服务
  • 有关做粪污处理设备的企业网站百度热搜电视剧
  • 怎么做网站主页seo的搜索排名影响因素有
  • 常熟做网站的公司seo怎么收费seo
  • 天津专业网站制作流程优势广告代发平台
  • 桦甸市建设局网站汽油价格最新调整最新消息
  • 深圳做营销网站公司简介app开发公司有哪些
  • 海南高端网站建设优化设计答案四年级上册语文
  • 郑州网站建设的公司哪家好交换链接适合哪些网站
  • html5可以做交互网站吗怎么做推广赚钱
  • 网站建设的具体方法企业网站seo公司
  • 国外wordpress周口seo推广